
#include "mpi_lib.h"
#include "mpi.h"
#include "mpi_cmd.h"
#include "mpi_isp.h"
#include "mpi_vi.h"
#include "mpi_sys.h"

static u32  G_sgks_isp_main_buffer_width     = 1920;
static u32  G_sgks_isp_main_buffer_height    = 1080;

int sgks_mpi_isp_Reset3A(sgks_mpi_manager_s *mpi)
{

   if (mpi->isp_init_param.enable != 1)
   {
		return 0;
   }
   
    int ret = -1;

    //usleep(10000);
    CHECK_POINT_NULL_RETURN(mpi, SGKS_FAIL);

    ret = gadi_isp_reset_3a_static(mpi->ispToImageHandle);
    if (ret != SGKS_SUCCESS)
    {
        return SGKS_ERR_ISP_RESET_3A_ERR;
    }


    ret = gadi_isp_set_wb_type(mpi->ispToImageHandle, 1);
    if (ret != SGKS_SUCCESS)
    {
        Printf("gadi_isp_set_wb_type 1 failed!\n");
        return SGKS_ERR_ISP_RESET_3A_ERR;
    }


    ret = gadi_isp_set_wb_type(mpi->ispToImageHandle, 0);
    if (ret != SGKS_SUCCESS)
    {
        Printf("gadi_isp_set_wb_type 0 failed!\n");
        return SGKS_ERR_ISP_RESET_3A_ERR;
    }

    Printf("sgks_mpi_isp_Reset3A ok\n");

    return SGKS_SUCCESS;

}


static int sgks_mpi_isp_CheckMemAddrPageAlign(sgks_isp_initParam_s *isp_config)
{

    int i = 0;

    u8 *msg_buf = NULL;
    SGKS_MDI_DRV_BUFF(msg_buf);
    SGKS_MDI_DRV_IOCTL(SGKS_MDI_OPERATION_GETPAGESIZE, 0, sizeof(sgks_get_pagesize_s), SGKS_ERR_SYS_DRIVERIOCTLERR);
    sgks_get_pagesize_s *kernel_page = msg_buf;
    u32 page_size = kernel_page->page_size;


    SGKS_MPI_CHECK_MEM_PAGE_ALIGN((u32)isp_config->isp_aaa_fifo1_base_phy, 					page_size, SGKS_ERR_SYS_INITMEMPARAMFAIL);
    SGKS_MPI_CHECK_MEM_PAGE_ALIGN((u32)isp_config->isp_aaa_fifo2_base_phy,			 		page_size, SGKS_ERR_SYS_INITMEMPARAMFAIL);
    SGKS_MPI_CHECK_MEM_PAGE_ALIGN((u32)isp_config->isp_input_lookup_table_phy, 				page_size, SGKS_ERR_SYS_INITMEMPARAMFAIL);
    SGKS_MPI_CHECK_MEM_PAGE_ALIGN((u32)isp_config->isp_matrix_dram_address_phy, 	   		page_size, SGKS_ERR_SYS_INITMEMPARAMFAIL);
    SGKS_MPI_CHECK_MEM_PAGE_ALIGN((u32)isp_config->isp_output_lookup_table_phy, 			page_size, SGKS_ERR_SYS_INITMEMPARAMFAIL);
    SGKS_MPI_CHECK_MEM_PAGE_ALIGN((u32)isp_config->isp_chroma_gain_curve_phy, 				page_size, SGKS_ERR_SYS_INITMEMPARAMFAIL);
    SGKS_MPI_CHECK_MEM_PAGE_ALIGN((u32)isp_config->isp_hot_pixel_thd_table_phy,			 	page_size, SGKS_ERR_SYS_INITMEMPARAMFAIL);
    SGKS_MPI_CHECK_MEM_PAGE_ALIGN((u32)isp_config->isp_dark_pixel_thd_table_phy, 			page_size, SGKS_ERR_SYS_INITMEMPARAMFAIL);
    SGKS_MPI_CHECK_MEM_PAGE_ALIGN((u32)isp_config->isp_mctf_cfg_addr_phy, 	   				page_size, SGKS_ERR_SYS_INITMEMPARAMFAIL);
    SGKS_MPI_CHECK_MEM_PAGE_ALIGN((u32)isp_config->isp_k0123_table_phy, 					page_size, SGKS_ERR_SYS_INITMEMPARAMFAIL);
    SGKS_MPI_CHECK_MEM_PAGE_ALIGN((u32)isp_config->isp_exposure_gain_curve_phy, 			page_size, SGKS_ERR_SYS_INITMEMPARAMFAIL);
    SGKS_MPI_CHECK_MEM_PAGE_ALIGN((u32)isp_config->isp_luma_sharpening_alpha_table_phy,		page_size, SGKS_ERR_SYS_INITMEMPARAMFAIL);
    SGKS_MPI_CHECK_MEM_PAGE_ALIGN((u32)isp_config->isp_coeff_fir1_addr_phy, 				page_size, SGKS_ERR_SYS_INITMEMPARAMFAIL);
    SGKS_MPI_CHECK_MEM_PAGE_ALIGN((u32)isp_config->isp_coeff_fir2_addr_phy, 	   			page_size, SGKS_ERR_SYS_INITMEMPARAMFAIL);
    SGKS_MPI_CHECK_MEM_PAGE_ALIGN((u32)isp_config->isp_coring_table_phy, 					page_size, SGKS_ERR_SYS_INITMEMPARAMFAIL);
    SGKS_MPI_CHECK_MEM_PAGE_ALIGN((u32)isp_config->isp_vignette_r_gain_phy, 				page_size, SGKS_ERR_SYS_INITMEMPARAMFAIL);
    SGKS_MPI_CHECK_MEM_PAGE_ALIGN((u32)isp_config->isp_vignette_go_gain_phy,			 	page_size, SGKS_ERR_SYS_INITMEMPARAMFAIL);
    SGKS_MPI_CHECK_MEM_PAGE_ALIGN((u32)isp_config->isp_vignette_ge_gain_phy, 				page_size, SGKS_ERR_SYS_INITMEMPARAMFAIL);
    SGKS_MPI_CHECK_MEM_PAGE_ALIGN((u32)isp_config->isp_vignette_b_gain_phy, 	   			page_size, SGKS_ERR_SYS_INITMEMPARAMFAIL);
    SGKS_MPI_CHECK_MEM_PAGE_ALIGN((u32)isp_config->isp_pixel_map_addr_phy, 					page_size, SGKS_ERR_SYS_INITMEMPARAMFAIL);
    SGKS_MPI_CHECK_MEM_PAGE_ALIGN((u32)isp_config->isp_fpn_reg_addr_phy, 					page_size, SGKS_ERR_SYS_INITMEMPARAMFAIL);
    SGKS_MPI_CHECK_MEM_PAGE_ALIGN((u32)isp_config->isp_hor_still_phy, 	   					page_size, SGKS_ERR_SYS_INITMEMPARAMFAIL);
    SGKS_MPI_CHECK_MEM_PAGE_ALIGN((u32)isp_config->isp_ver_still_phy, 						page_size, SGKS_ERR_SYS_INITMEMPARAMFAIL);
    SGKS_MPI_CHECK_MEM_PAGE_ALIGN((u32)isp_config->isp_eis_enhance_turbo_buf_phy, 			page_size, SGKS_ERR_SYS_INITMEMPARAMFAIL);


    return SGKS_SUCCESS;

}



int sgks_mpi_isp_UnMmapPhyAddr(sgks_isp_initParam_s * isp_config)
{
    u8 *msg_buf = NULL;
    SGKS_MDI_DRV_BUFF(msg_buf);
    sgks_mdi_mmap_s *mmap_info = msg_buf;

    SGKS_MDI_DRV_CLEAN_DATABUFF();
    mmap_info->mmap_user_start_addr = (u8 *)isp_config->isp_aaa_fifo1_base_vir;
    mmap_info->mmap_user_size       = (int)isp_config->isp_aaa_fifo1_base_size;
    SGKS_MDI_DRV_IOCTL(SGKS_MDI_OPERATION_UNMMAP, 0, sizeof(sgks_mdi_mmap_s), SGKS_ERR_SYS_DRIVERIOCTLERR);
     
    Printf("isp unmap: phy addr 0x%x ----> vir 0x%x    size 0x%x\n",isp_config->isp_aaa_fifo1_base_phy, mmap_info->mmap_user_size);

    SGKS_MDI_DRV_CLEAN_DATABUFF();
    mmap_info->mmap_user_start_addr = (u8 *)isp_config->isp_aaa_fifo2_base_vir;
    mmap_info->mmap_user_size       = (int)isp_config->isp_aaa_fifo2_base_size;
    SGKS_MDI_DRV_IOCTL(SGKS_MDI_OPERATION_UNMMAP, 0, sizeof(sgks_mdi_mmap_s), SGKS_ERR_SYS_DRIVERIOCTLERR);
    
    SGKS_MDI_DRV_CLEAN_DATABUFF();
    mmap_info->mmap_user_start_addr = (u8 *)isp_config->isp_input_lookup_table_vir;
    mmap_info->mmap_user_size       = (int)isp_config->isp_input_lookup_table_size;
    SGKS_MDI_DRV_IOCTL(SGKS_MDI_OPERATION_UNMMAP, 0, sizeof(sgks_mdi_mmap_s), SGKS_ERR_SYS_DRIVERIOCTLERR);
    
    SGKS_MDI_DRV_CLEAN_DATABUFF();
    mmap_info->mmap_user_start_addr = (u8 *)isp_config->isp_matrix_dram_address_vir;
    mmap_info->mmap_user_size       = (int)isp_config->isp_matrix_dram_address_size;
    SGKS_MDI_DRV_IOCTL(SGKS_MDI_OPERATION_UNMMAP, 0, sizeof(sgks_mdi_mmap_s), SGKS_ERR_SYS_DRIVERIOCTLERR);
    
    SGKS_MDI_DRV_CLEAN_DATABUFF();
    mmap_info->mmap_user_start_addr = (u8 *)isp_config->isp_output_lookup_table_vir;
    mmap_info->mmap_user_size       = (int)isp_config->isp_output_lookup_table_size;
    SGKS_MDI_DRV_IOCTL(SGKS_MDI_OPERATION_UNMMAP, 0, sizeof(sgks_mdi_mmap_s), SGKS_ERR_SYS_DRIVERIOCTLERR);
    
    SGKS_MDI_DRV_CLEAN_DATABUFF();
    mmap_info->mmap_user_start_addr = (u8 *)isp_config->isp_chroma_gain_curve_vir;
    mmap_info->mmap_user_size       = (int)isp_config->isp_chroma_gain_curve_size;
    SGKS_MDI_DRV_IOCTL(SGKS_MDI_OPERATION_UNMMAP, 0, sizeof(sgks_mdi_mmap_s), SGKS_ERR_SYS_DRIVERIOCTLERR);
    
    SGKS_MDI_DRV_CLEAN_DATABUFF();
    mmap_info->mmap_user_start_addr = (u8 *)isp_config->isp_hot_pixel_thd_table_vir;
    mmap_info->mmap_user_size       = (int)isp_config->isp_hot_pixel_thd_table_size;
    SGKS_MDI_DRV_IOCTL(SGKS_MDI_OPERATION_UNMMAP, 0, sizeof(sgks_mdi_mmap_s), SGKS_ERR_SYS_DRIVERIOCTLERR);
    
    SGKS_MDI_DRV_CLEAN_DATABUFF();
    mmap_info->mmap_user_start_addr = (u8 *)isp_config->isp_dark_pixel_thd_table_vir;
    mmap_info->mmap_user_size       = (int)isp_config->isp_dark_pixel_thd_table_size;
    SGKS_MDI_DRV_IOCTL(SGKS_MDI_OPERATION_UNMMAP, 0, sizeof(sgks_mdi_mmap_s), SGKS_ERR_SYS_DRIVERIOCTLERR);
    
    SGKS_MDI_DRV_CLEAN_DATABUFF();
    mmap_info->mmap_user_start_addr = (u8 *)isp_config->isp_mctf_cfg_addr_vir;
    mmap_info->mmap_user_size       = (int)isp_config->isp_mctf_cfg_addr_size;
    SGKS_MDI_DRV_IOCTL(SGKS_MDI_OPERATION_UNMMAP, 0, sizeof(sgks_mdi_mmap_s), SGKS_ERR_SYS_DRIVERIOCTLERR);
    
    SGKS_MDI_DRV_CLEAN_DATABUFF();
    mmap_info->mmap_user_start_addr = (u8 *)isp_config->isp_k0123_table_vir;
    mmap_info->mmap_user_size       = (int)isp_config->isp_k0123_table_size;
    SGKS_MDI_DRV_IOCTL(SGKS_MDI_OPERATION_UNMMAP, 0, sizeof(sgks_mdi_mmap_s), SGKS_ERR_SYS_DRIVERIOCTLERR);
    
    SGKS_MDI_DRV_CLEAN_DATABUFF();
    mmap_info->mmap_user_start_addr = (u8 *)isp_config->isp_exposure_gain_curve_vir;
    mmap_info->mmap_user_size       = (int)isp_config->isp_exposure_gain_curve_size;
    SGKS_MDI_DRV_IOCTL(SGKS_MDI_OPERATION_UNMMAP, 0, sizeof(sgks_mdi_mmap_s), SGKS_ERR_SYS_DRIVERIOCTLERR);
    
    SGKS_MDI_DRV_CLEAN_DATABUFF();
    mmap_info->mmap_user_start_addr = (u8 *)isp_config->isp_luma_sharpening_alpha_table_vir;
    mmap_info->mmap_user_size       = (int)isp_config->isp_luma_sharpening_alpha_table_size;
    SGKS_MDI_DRV_IOCTL(SGKS_MDI_OPERATION_UNMMAP, 0, sizeof(sgks_mdi_mmap_s), SGKS_ERR_SYS_DRIVERIOCTLERR);
    
    SGKS_MDI_DRV_CLEAN_DATABUFF();
    mmap_info->mmap_user_start_addr = (u8 *)isp_config->isp_coeff_fir1_addr_vir;
    mmap_info->mmap_user_size       = (int)isp_config->isp_coeff_fir1_addr_size;
    SGKS_MDI_DRV_IOCTL(SGKS_MDI_OPERATION_UNMMAP, 0, sizeof(sgks_mdi_mmap_s), SGKS_ERR_SYS_DRIVERIOCTLERR);
    
    SGKS_MDI_DRV_CLEAN_DATABUFF();
    mmap_info->mmap_user_start_addr = (u8 *)isp_config->isp_coeff_fir2_addr_vir;
    mmap_info->mmap_user_size       = (int)isp_config->isp_coeff_fir2_addr_size;
    SGKS_MDI_DRV_IOCTL(SGKS_MDI_OPERATION_UNMMAP, 0, sizeof(sgks_mdi_mmap_s), SGKS_ERR_SYS_DRIVERIOCTLERR);
    
    SGKS_MDI_DRV_CLEAN_DATABUFF();
    mmap_info->mmap_user_start_addr = (u8 *)isp_config->isp_coring_table_vir;
    mmap_info->mmap_user_size       = (int)isp_config->isp_coring_table_size;
    SGKS_MDI_DRV_IOCTL(SGKS_MDI_OPERATION_UNMMAP, 0, sizeof(sgks_mdi_mmap_s), SGKS_ERR_SYS_DRIVERIOCTLERR);
    
    SGKS_MDI_DRV_CLEAN_DATABUFF();
    mmap_info->mmap_user_start_addr = (u8 *)isp_config->isp_vignette_r_gain_vir;
    mmap_info->mmap_user_size       = (int)isp_config->isp_vignette_r_gain_size;
    SGKS_MDI_DRV_IOCTL(SGKS_MDI_OPERATION_UNMMAP, 0, sizeof(sgks_mdi_mmap_s), SGKS_ERR_SYS_DRIVERIOCTLERR);
    
    SGKS_MDI_DRV_CLEAN_DATABUFF();
    mmap_info->mmap_user_start_addr = (u8 *)isp_config->isp_vignette_go_gain_vir;
    mmap_info->mmap_user_size       = (int)isp_config->isp_vignette_go_gain_size;
    SGKS_MDI_DRV_IOCTL(SGKS_MDI_OPERATION_UNMMAP, 0, sizeof(sgks_mdi_mmap_s), SGKS_ERR_SYS_DRIVERIOCTLERR);
    
    SGKS_MDI_DRV_CLEAN_DATABUFF();
    mmap_info->mmap_user_start_addr = (u8 *)isp_config->isp_vignette_ge_gain_vir;
    mmap_info->mmap_user_size       = (int)isp_config->isp_vignette_ge_gain_size;
    SGKS_MDI_DRV_IOCTL(SGKS_MDI_OPERATION_UNMMAP, 0, sizeof(sgks_mdi_mmap_s), SGKS_ERR_SYS_DRIVERIOCTLERR);
    
    SGKS_MDI_DRV_CLEAN_DATABUFF();
    mmap_info->mmap_user_start_addr = (u8 *)isp_config->isp_vignette_b_gain_vir;
    mmap_info->mmap_user_size       = (int)isp_config->isp_vignette_b_gain_size;
    SGKS_MDI_DRV_IOCTL(SGKS_MDI_OPERATION_UNMMAP, 0, sizeof(sgks_mdi_mmap_s), SGKS_ERR_SYS_DRIVERIOCTLERR);
    
    SGKS_MDI_DRV_CLEAN_DATABUFF();
    mmap_info->mmap_user_start_addr = (u8 *)isp_config->isp_pixel_map_addr_vir;
    mmap_info->mmap_user_size       = (int)isp_config->isp_pixel_map_addr_size;
    SGKS_MDI_DRV_IOCTL(SGKS_MDI_OPERATION_UNMMAP, 0, sizeof(sgks_mdi_mmap_s), SGKS_ERR_SYS_DRIVERIOCTLERR);
    
    SGKS_MDI_DRV_CLEAN_DATABUFF();
    mmap_info->mmap_user_start_addr = (u8 *)isp_config->isp_fpn_reg_addr_vir;
    mmap_info->mmap_user_size       = (int)isp_config->isp_fpn_reg_addr_size;
    SGKS_MDI_DRV_IOCTL(SGKS_MDI_OPERATION_UNMMAP, 0, sizeof(sgks_mdi_mmap_s), SGKS_ERR_SYS_DRIVERIOCTLERR);
    
    SGKS_MDI_DRV_CLEAN_DATABUFF();
    mmap_info->mmap_user_start_addr = (u8 *)isp_config->isp_hor_still_vir;
    mmap_info->mmap_user_size       = (int)isp_config->isp_hor_still_size;
    SGKS_MDI_DRV_IOCTL(SGKS_MDI_OPERATION_UNMMAP, 0, sizeof(sgks_mdi_mmap_s), SGKS_ERR_SYS_DRIVERIOCTLERR);
    
    SGKS_MDI_DRV_CLEAN_DATABUFF();
    mmap_info->mmap_user_start_addr = (u8 *)isp_config->isp_ver_still_vir;
    mmap_info->mmap_user_size       = (int)isp_config->isp_ver_still_size;
    SGKS_MDI_DRV_IOCTL(SGKS_MDI_OPERATION_UNMMAP, 0, sizeof(sgks_mdi_mmap_s), SGKS_ERR_SYS_DRIVERIOCTLERR);
    
    SGKS_MDI_DRV_CLEAN_DATABUFF();
    mmap_info->mmap_user_start_addr = (u8 *)isp_config->isp_eis_enhance_turbo_buf_vir;
    mmap_info->mmap_user_size       = (int)isp_config->isp_eis_enhance_turbo_buf_size;
    SGKS_MDI_DRV_IOCTL(SGKS_MDI_OPERATION_UNMMAP, 0, sizeof(sgks_mdi_mmap_s), SGKS_ERR_SYS_DRIVERIOCTLERR);

	Printf("isp unmap ok\n");


    return SGKS_SUCCESS;

}



int sgks_mpi_isp_MmapPhyAddr(sgks_isp_initParam_s * isp_config)
{
    u8 *msg_buf = NULL;
    SGKS_MDI_DRV_BUFF(msg_buf);
    sgks_mdi_mmap_s *mmap_info = msg_buf;

    SGKS_MDI_DRV_CLEAN_DATABUFF();
    mmap_info->mmap_phy_start_addr = (u8 *)isp_config->isp_aaa_fifo1_base_phy;
    mmap_info->mmap_phy_size       = (int)isp_config->isp_aaa_fifo1_base_size;
    SGKS_MDI_DRV_IOCTL(SGKS_MDI_OPERATION_MMAP, 0, sizeof(sgks_mdi_mmap_s), SGKS_ERR_SYS_DRIVERIOCTLERR);
    isp_config->isp_aaa_fifo1_base_vir = (u32 *)mmap_info->mmap_user_start_addr;
    Printf("isp map: phy addr 0x%x ----> vir 0x%x    size 0x%x\n",isp_config->isp_aaa_fifo1_base_phy, mmap_info->mmap_user_start_addr, mmap_info->mmap_user_size);

    SGKS_MDI_DRV_CLEAN_DATABUFF();
    mmap_info->mmap_phy_start_addr = (u8 *)isp_config->isp_aaa_fifo2_base_phy;
    mmap_info->mmap_phy_size       = (int)isp_config->isp_aaa_fifo2_base_size;
    SGKS_MDI_DRV_IOCTL(SGKS_MDI_OPERATION_MMAP, 0, sizeof(sgks_mdi_mmap_s), SGKS_ERR_SYS_DRIVERIOCTLERR);
    isp_config->isp_aaa_fifo2_base_vir = (u32 *)mmap_info->mmap_user_start_addr;
    Printf("isp map: phy addr 0x%x ----> vir 0x%x    size 0x%x\n",isp_config->isp_aaa_fifo2_base_phy, mmap_info->mmap_user_start_addr, mmap_info->mmap_user_size);

    SGKS_MDI_DRV_CLEAN_DATABUFF();
    mmap_info->mmap_phy_start_addr = (u8 *)isp_config->isp_input_lookup_table_phy;
    mmap_info->mmap_phy_size       = (int)isp_config->isp_input_lookup_table_size;
    SGKS_MDI_DRV_IOCTL(SGKS_MDI_OPERATION_MMAP, 0, sizeof(sgks_mdi_mmap_s), SGKS_ERR_SYS_DRIVERIOCTLERR);
    isp_config->isp_input_lookup_table_vir = (u32 *)mmap_info->mmap_user_start_addr;
    Printf("isp map: phy addr 0x%x ----> vir 0x%x    size 0x%x\n",isp_config->isp_input_lookup_table_phy, mmap_info->mmap_user_start_addr, mmap_info->mmap_user_size);

    SGKS_MDI_DRV_CLEAN_DATABUFF();
    mmap_info->mmap_phy_start_addr = (u8 *)isp_config->isp_matrix_dram_address_phy;
    mmap_info->mmap_phy_size       = (int)isp_config->isp_matrix_dram_address_size;
    SGKS_MDI_DRV_IOCTL(SGKS_MDI_OPERATION_MMAP, 0, sizeof(sgks_mdi_mmap_s), SGKS_ERR_SYS_DRIVERIOCTLERR);
    isp_config->isp_matrix_dram_address_vir = (u32 *)mmap_info->mmap_user_start_addr;
    Printf("isp map: phy addr 0x%x ----> vir 0x%x    size 0x%x\n",isp_config->isp_matrix_dram_address_phy, mmap_info->mmap_user_start_addr, mmap_info->mmap_user_size);

    SGKS_MDI_DRV_CLEAN_DATABUFF();
    mmap_info->mmap_phy_start_addr = (u8 *)isp_config->isp_output_lookup_table_phy;
    mmap_info->mmap_phy_size       = (int)isp_config->isp_output_lookup_table_size;
    SGKS_MDI_DRV_IOCTL(SGKS_MDI_OPERATION_MMAP, 0, sizeof(sgks_mdi_mmap_s), SGKS_ERR_SYS_DRIVERIOCTLERR);
    isp_config->isp_output_lookup_table_vir = (u32 *)mmap_info->mmap_user_start_addr;
    Printf("isp map: phy addr 0x%x ----> vir 0x%x    size 0x%x\n",isp_config->isp_output_lookup_table_phy, mmap_info->mmap_user_start_addr, mmap_info->mmap_user_size);

    SGKS_MDI_DRV_CLEAN_DATABUFF();
    mmap_info->mmap_phy_start_addr = (u8 *)isp_config->isp_chroma_gain_curve_phy;
    mmap_info->mmap_phy_size       = (int)isp_config->isp_chroma_gain_curve_size;
    SGKS_MDI_DRV_IOCTL(SGKS_MDI_OPERATION_MMAP, 0, sizeof(sgks_mdi_mmap_s), SGKS_ERR_SYS_DRIVERIOCTLERR);
    isp_config->isp_chroma_gain_curve_vir = (u32 *)mmap_info->mmap_user_start_addr;
    Printf("isp map: phy addr 0x%x ----> vir 0x%x    size 0x%x\n",isp_config->isp_chroma_gain_curve_phy, mmap_info->mmap_user_start_addr, mmap_info->mmap_user_size);

    SGKS_MDI_DRV_CLEAN_DATABUFF();
    mmap_info->mmap_phy_start_addr = (u8 *)isp_config->isp_hot_pixel_thd_table_phy;
    mmap_info->mmap_phy_size       = (int)isp_config->isp_hot_pixel_thd_table_size;
    SGKS_MDI_DRV_IOCTL(SGKS_MDI_OPERATION_MMAP, 0, sizeof(sgks_mdi_mmap_s), SGKS_ERR_SYS_DRIVERIOCTLERR);
    isp_config->isp_hot_pixel_thd_table_vir = (u32 *)mmap_info->mmap_user_start_addr;
    Printf("isp map: phy addr 0x%x ----> vir 0x%x    size 0x%x\n",isp_config->isp_hot_pixel_thd_table_phy, mmap_info->mmap_user_start_addr, mmap_info->mmap_user_size);

    SGKS_MDI_DRV_CLEAN_DATABUFF();
    mmap_info->mmap_phy_start_addr = (u8 *)isp_config->isp_dark_pixel_thd_table_phy;
    mmap_info->mmap_phy_size       = (int)isp_config->isp_dark_pixel_thd_table_size;
    SGKS_MDI_DRV_IOCTL(SGKS_MDI_OPERATION_MMAP, 0, sizeof(sgks_mdi_mmap_s), SGKS_ERR_SYS_DRIVERIOCTLERR);
    isp_config->isp_dark_pixel_thd_table_vir = (u32 *)mmap_info->mmap_user_start_addr;
    Printf("isp map: phy addr 0x%x ----> vir 0x%x    size 0x%x\n",isp_config->isp_dark_pixel_thd_table_phy, mmap_info->mmap_user_start_addr, mmap_info->mmap_user_size);

    SGKS_MDI_DRV_CLEAN_DATABUFF();
    mmap_info->mmap_phy_start_addr = (u8 *)isp_config->isp_mctf_cfg_addr_phy;
    mmap_info->mmap_phy_size       = (int)isp_config->isp_mctf_cfg_addr_size;
    SGKS_MDI_DRV_IOCTL(SGKS_MDI_OPERATION_MMAP, 0, sizeof(sgks_mdi_mmap_s), SGKS_ERR_SYS_DRIVERIOCTLERR);
    isp_config->isp_mctf_cfg_addr_vir = (u32 *)mmap_info->mmap_user_start_addr;
    Printf("isp map: phy addr 0x%x ----> vir 0x%x    size 0x%x\n",isp_config->isp_mctf_cfg_addr_phy, mmap_info->mmap_user_start_addr, mmap_info->mmap_user_size);

    SGKS_MDI_DRV_CLEAN_DATABUFF();
    mmap_info->mmap_phy_start_addr = (u8 *)isp_config->isp_k0123_table_phy;
    mmap_info->mmap_phy_size       = (int)isp_config->isp_k0123_table_size;
    SGKS_MDI_DRV_IOCTL(SGKS_MDI_OPERATION_MMAP, 0, sizeof(sgks_mdi_mmap_s), SGKS_ERR_SYS_DRIVERIOCTLERR);
    isp_config->isp_k0123_table_vir = (u32 *)mmap_info->mmap_user_start_addr;
    Printf("isp map: phy addr 0x%x ----> vir 0x%x    size 0x%x\n",isp_config->isp_k0123_table_phy, mmap_info->mmap_user_start_addr, mmap_info->mmap_user_size);

    SGKS_MDI_DRV_CLEAN_DATABUFF();
    mmap_info->mmap_phy_start_addr = (u8 *)isp_config->isp_exposure_gain_curve_phy;
    mmap_info->mmap_phy_size       = (int)isp_config->isp_exposure_gain_curve_size;
    SGKS_MDI_DRV_IOCTL(SGKS_MDI_OPERATION_MMAP, 0, sizeof(sgks_mdi_mmap_s), SGKS_ERR_SYS_DRIVERIOCTLERR);
    isp_config->isp_exposure_gain_curve_vir = (u32 *)mmap_info->mmap_user_start_addr;
    Printf("isp map: phy addr 0x%x ----> vir 0x%x    size 0x%x\n",isp_config->isp_exposure_gain_curve_phy, mmap_info->mmap_user_start_addr, mmap_info->mmap_user_size);

    SGKS_MDI_DRV_CLEAN_DATABUFF();
    mmap_info->mmap_phy_start_addr = (u8 *)isp_config->isp_luma_sharpening_alpha_table_phy;
    mmap_info->mmap_phy_size       = (int)isp_config->isp_luma_sharpening_alpha_table_size;
    SGKS_MDI_DRV_IOCTL(SGKS_MDI_OPERATION_MMAP, 0, sizeof(sgks_mdi_mmap_s), SGKS_ERR_SYS_DRIVERIOCTLERR);
    isp_config->isp_luma_sharpening_alpha_table_vir = (u32 *)mmap_info->mmap_user_start_addr;
    Printf("isp map: phy addr 0x%x ----> vir 0x%x    size 0x%x\n",isp_config->isp_luma_sharpening_alpha_table_phy, mmap_info->mmap_user_start_addr, mmap_info->mmap_user_size);

    SGKS_MDI_DRV_CLEAN_DATABUFF();
    mmap_info->mmap_phy_start_addr = (u8 *)isp_config->isp_coeff_fir1_addr_phy;
    mmap_info->mmap_phy_size       = (int)isp_config->isp_coeff_fir1_addr_size;
    SGKS_MDI_DRV_IOCTL(SGKS_MDI_OPERATION_MMAP, 0, sizeof(sgks_mdi_mmap_s), SGKS_ERR_SYS_DRIVERIOCTLERR);
    isp_config->isp_coeff_fir1_addr_vir = (u32 *)mmap_info->mmap_user_start_addr;
    Printf("isp map: phy addr 0x%x ----> vir 0x%x    size 0x%x\n",isp_config->isp_coeff_fir1_addr_phy, mmap_info->mmap_user_start_addr, mmap_info->mmap_user_size);

    SGKS_MDI_DRV_CLEAN_DATABUFF();
    mmap_info->mmap_phy_start_addr = (u8 *)isp_config->isp_coeff_fir2_addr_phy;
    mmap_info->mmap_phy_size       = (int)isp_config->isp_coeff_fir2_addr_size;
    SGKS_MDI_DRV_IOCTL(SGKS_MDI_OPERATION_MMAP, 0, sizeof(sgks_mdi_mmap_s), SGKS_ERR_SYS_DRIVERIOCTLERR);
    isp_config->isp_coeff_fir2_addr_vir = (u32 *)mmap_info->mmap_user_start_addr;
    Printf("isp map: phy addr 0x%x ----> vir 0x%x    size 0x%x\n",isp_config->isp_coeff_fir2_addr_phy, mmap_info->mmap_user_start_addr, mmap_info->mmap_user_size);

    SGKS_MDI_DRV_CLEAN_DATABUFF();
    mmap_info->mmap_phy_start_addr = (u8 *)isp_config->isp_coring_table_phy;
    mmap_info->mmap_phy_size       = (int)isp_config->isp_coring_table_size;
    SGKS_MDI_DRV_IOCTL(SGKS_MDI_OPERATION_MMAP, 0, sizeof(sgks_mdi_mmap_s), SGKS_ERR_SYS_DRIVERIOCTLERR);
    isp_config->isp_coring_table_vir = (u32 *)mmap_info->mmap_user_start_addr;
    Printf("isp map: phy addr 0x%x ----> vir 0x%x    size 0x%x\n",isp_config->isp_coring_table_phy, mmap_info->mmap_user_start_addr, mmap_info->mmap_user_size);

    SGKS_MDI_DRV_CLEAN_DATABUFF();
    mmap_info->mmap_phy_start_addr = (u8 *)isp_config->isp_vignette_r_gain_phy;
    mmap_info->mmap_phy_size       = (int)isp_config->isp_vignette_r_gain_size;
    SGKS_MDI_DRV_IOCTL(SGKS_MDI_OPERATION_MMAP, 0, sizeof(sgks_mdi_mmap_s), SGKS_ERR_SYS_DRIVERIOCTLERR);
    isp_config->isp_vignette_r_gain_vir = (u32 *)mmap_info->mmap_user_start_addr;
    Printf("isp map: phy addr 0x%x ----> vir 0x%x    size 0x%x\n",isp_config->isp_vignette_r_gain_phy, mmap_info->mmap_user_start_addr, mmap_info->mmap_user_size);

    SGKS_MDI_DRV_CLEAN_DATABUFF();
    mmap_info->mmap_phy_start_addr = (u8 *)isp_config->isp_vignette_go_gain_phy;
    mmap_info->mmap_phy_size       = (int)isp_config->isp_vignette_go_gain_size;
    SGKS_MDI_DRV_IOCTL(SGKS_MDI_OPERATION_MMAP, 0, sizeof(sgks_mdi_mmap_s), SGKS_ERR_SYS_DRIVERIOCTLERR);
    isp_config->isp_vignette_go_gain_vir = (u32 *)mmap_info->mmap_user_start_addr;
    Printf("isp map: phy addr 0x%x ----> vir 0x%x    size 0x%x\n",isp_config->isp_vignette_go_gain_phy, mmap_info->mmap_user_start_addr, mmap_info->mmap_user_size);

    SGKS_MDI_DRV_CLEAN_DATABUFF();
    mmap_info->mmap_phy_start_addr = (u8 *)isp_config->isp_vignette_ge_gain_phy;
    mmap_info->mmap_phy_size       = (int)isp_config->isp_vignette_ge_gain_size;
    SGKS_MDI_DRV_IOCTL(SGKS_MDI_OPERATION_MMAP, 0, sizeof(sgks_mdi_mmap_s), SGKS_ERR_SYS_DRIVERIOCTLERR);
    isp_config->isp_vignette_ge_gain_vir = (u32 *)mmap_info->mmap_user_start_addr;
    Printf("isp map: phy addr 0x%x ----> vir 0x%x    size 0x%x\n",isp_config->isp_vignette_ge_gain_phy, mmap_info->mmap_user_start_addr, mmap_info->mmap_user_size);

    SGKS_MDI_DRV_CLEAN_DATABUFF();
    mmap_info->mmap_phy_start_addr = (u8 *)isp_config->isp_vignette_b_gain_phy;
    mmap_info->mmap_phy_size       = (int)isp_config->isp_vignette_b_gain_size;
    SGKS_MDI_DRV_IOCTL(SGKS_MDI_OPERATION_MMAP, 0, sizeof(sgks_mdi_mmap_s), SGKS_ERR_SYS_DRIVERIOCTLERR);
    isp_config->isp_vignette_b_gain_vir = (u32 *)mmap_info->mmap_user_start_addr;
    Printf("isp map: phy addr 0x%x ----> vir 0x%x    size 0x%x\n",isp_config->isp_vignette_b_gain_phy, mmap_info->mmap_user_start_addr, mmap_info->mmap_user_size);

    SGKS_MDI_DRV_CLEAN_DATABUFF();
    mmap_info->mmap_phy_start_addr = (u8 *)isp_config->isp_pixel_map_addr_phy;
    mmap_info->mmap_phy_size       = (int)isp_config->isp_pixel_map_addr_size;
    SGKS_MDI_DRV_IOCTL(SGKS_MDI_OPERATION_MMAP, 0, sizeof(sgks_mdi_mmap_s), SGKS_ERR_SYS_DRIVERIOCTLERR);
    isp_config->isp_pixel_map_addr_vir = (u32 *)mmap_info->mmap_user_start_addr;
    Printf("isp map: phy addr 0x%x ----> vir 0x%x    size 0x%x\n",isp_config->isp_pixel_map_addr_phy, mmap_info->mmap_user_start_addr, mmap_info->mmap_user_size);

    SGKS_MDI_DRV_CLEAN_DATABUFF();
    mmap_info->mmap_phy_start_addr = (u8 *)isp_config->isp_fpn_reg_addr_phy;
    mmap_info->mmap_phy_size       = (int)isp_config->isp_fpn_reg_addr_size;
    SGKS_MDI_DRV_IOCTL(SGKS_MDI_OPERATION_MMAP, 0, sizeof(sgks_mdi_mmap_s), SGKS_ERR_SYS_DRIVERIOCTLERR);
    isp_config->isp_fpn_reg_addr_vir = (u32 *)mmap_info->mmap_user_start_addr;
    Printf("isp map: phy addr 0x%x ----> vir 0x%x    size 0x%x\n",isp_config->isp_fpn_reg_addr_phy, mmap_info->mmap_user_start_addr, mmap_info->mmap_user_size);

    SGKS_MDI_DRV_CLEAN_DATABUFF();
    mmap_info->mmap_phy_start_addr = (u8 *)isp_config->isp_hor_still_phy;
    mmap_info->mmap_phy_size       = (int)isp_config->isp_hor_still_size;
    SGKS_MDI_DRV_IOCTL(SGKS_MDI_OPERATION_MMAP, 0, sizeof(sgks_mdi_mmap_s), SGKS_ERR_SYS_DRIVERIOCTLERR);
    isp_config->isp_hor_still_vir = (u32 *)mmap_info->mmap_user_start_addr;
    Printf("isp map: phy addr 0x%x ----> vir 0x%x    size 0x%x\n",isp_config->isp_hor_still_phy, mmap_info->mmap_user_start_addr, mmap_info->mmap_user_size);

    SGKS_MDI_DRV_CLEAN_DATABUFF();
    mmap_info->mmap_phy_start_addr = (u8 *)isp_config->isp_ver_still_phy;
    mmap_info->mmap_phy_size       = (int)isp_config->isp_ver_still_size;
    SGKS_MDI_DRV_IOCTL(SGKS_MDI_OPERATION_MMAP, 0, sizeof(sgks_mdi_mmap_s), SGKS_ERR_SYS_DRIVERIOCTLERR);
    isp_config->isp_ver_still_vir = (u32 *)mmap_info->mmap_user_start_addr;
    Printf("isp map: phy addr 0x%x ----> vir 0x%x    size 0x%x\n",isp_config->isp_ver_still_phy, mmap_info->mmap_user_start_addr, mmap_info->mmap_user_size);

    SGKS_MDI_DRV_CLEAN_DATABUFF();
    mmap_info->mmap_phy_start_addr = (u8 *)isp_config->isp_eis_enhance_turbo_buf_phy;
    mmap_info->mmap_phy_size       = (int)isp_config->isp_eis_enhance_turbo_buf_size;
    SGKS_MDI_DRV_IOCTL(SGKS_MDI_OPERATION_MMAP, 0, sizeof(sgks_mdi_mmap_s), SGKS_ERR_SYS_DRIVERIOCTLERR);
    isp_config->isp_eis_enhance_turbo_buf_vir = (u32 *)mmap_info->mmap_user_start_addr;
    Printf("isp map: phy addr 0x%x ----> vir 0x%x    size 0x%x\n",isp_config->isp_eis_enhance_turbo_buf_phy, mmap_info->mmap_user_start_addr, mmap_info->mmap_user_size);



    return SGKS_SUCCESS;

}



static int sgks_isp_CheckFwStatus()
{
//for fw
#if 0
    if(!(g_info->state == MEDIA_STATE_PREVIEW ||
            g_info->state == MEDIA_STATE_ENCODING ||
            g_info->state == MEDIA_STATE_PREVIEW_STARTING ||
            g_info->state == MEDIA_STATE_ENCODE_STARTING ||
            g_info->state == MEDIA_STATE_ENCODE_STOPPING))
    {
        return -1;
    }
#endif

    return SGKS_SUCCESS;
}




static int sgks_isp_cmd_GetStatistics(sgks_mpi_manager_s *mpi, struct isp_statistics  *arg)
{

    u8 *msg_buf = NULL;
    SGKS_MDI_DRV_BUFF(msg_buf);

    struct isp_statistics aaa_info;
    struct cfa_aaa_stat_s *pCfa;
    struct rgb_aaa_stat_s *pRgb;

    memcpy(&aaa_info, arg, sizeof(struct isp_statistics));

    if(aaa_info.rgb_statis == NULL || aaa_info.rgb_data_valid == NULL
            || aaa_info.cfa_statis == NULL || aaa_info.cfa_data_valid == NULL
            || aaa_info.hist_statis == NULL || aaa_info.hist_data_valid == NULL)
    {
        Printf("Invalid pointer from user app!\n");
        return SGKS_ERR_ISP_STATISTICS_FAIL;
    }


    *(aaa_info.rgb_data_valid) 		= 0;
    *(aaa_info.cfa_data_valid) 		= 0;
    *(aaa_info.hist_data_valid)     = 0;

    u8* aaa_fifo2_base 										= (u8*)mpi->isp_init_param.isp_aaa_fifo2_base_vir;
    sgks_mdi_isp_statistics_pos_s *drv_isp_statistics_pos 	= msg_buf;

    SGKS_MDI_DRV_IOCTL(SGKS_MDI_OPERATION_GET_ISP_STATISTICS, 0, sizeof(sgks_mdi_isp_statistics_pos_s), SGKS_ERR_SYS_DRIVERIOCTLERR);

    u32 isp_rgb_aaa_ptr_pos  = drv_isp_statistics_pos->isp_rgb_aaa_ptr_pos;
    u32 isp_cfa_aaa_ptr_pos  = drv_isp_statistics_pos->isp_cfa_aaa_ptr_pos;




    /*-----------------------------------isp_rgb_aaa_ptr--------------------------------------*/
#if 0
    Printf("**************isp_rgb_aaa_ptr****************\n");
    Printf("isp_rgb_aaa_ptr pos %d\n",isp_rgb_aaa_ptr_pos);
#endif

    pRgb = (struct rgb_aaa_stat_s *)((u8 *)mpi->isp_init_param.isp_aaa_fifo1_base_vir + isp_rgb_aaa_ptr_pos);


#if 0
    SGKS_MDI_DRV_CLEAN_DATABUFF();
    sgks_mdi_opt_d_cache_s *cache 			= msg_buf;
    cache->cache_type                		= SGKS_OPT_D_CACHE_INV;
    cache->start                     		= (u8 *)mpi->isp_init_param.isp_aaa_fifo1_base_vir + isp_rgb_aaa_ptr_pos;
    cache->size                      		= RGB_AAA_DATA_BLOCK;
    SGKS_MDI_DRV_IOCTL(SGKS_MDI_OPERATION_OPT_D_CACHE, 0, sizeof(sgks_mdi_opt_d_cache_s), SGKS_ERR_SYS_DRIVERIOCTLERR);
#endif


    memcpy(aaa_info.rgb_statis, pRgb, RGB_AAA_DATA_BLOCK);
    *(aaa_info.rgb_data_valid) = 1;

#if 0
    Printf("RGB Hist bin [32] %d %d %d %d.\n"
           ,pRgb->histogram_stat.his_bin_y[32]
           ,pRgb->histogram_stat.his_bin_r[32]
           ,pRgb->histogram_stat.his_bin_g[32]
           ,pRgb->histogram_stat.his_bin_b[32]);

    Printf("pRgb->ae_sum_y  %d %d %d %d.\n"
           ,pRgb->ae_sum_y[0]
           ,pRgb->ae_sum_y[1]
           ,pRgb->ae_sum_y[2]
           ,pRgb->ae_sum_y[3]);
#endif

    /*---------------------------------isp_cfa_aaa_ptr----------------------------------------*/
#if 0
    Printf("**************isp_cfa_aaa_ptr****************\n");
    Printf("isp_cfa_aaa_ptr pos %d\n",isp_cfa_aaa_ptr_pos);
#endif

    pCfa = (struct cfa_aaa_stat_s *)((u8*)mpi->isp_init_param.isp_aaa_fifo2_base_vir + isp_cfa_aaa_ptr_pos);

#if 0
    SGKS_MDI_DRV_CLEAN_DATABUFF();
    cache 									= msg_buf;
    cache->cache_type                		= SGKS_OPT_D_CACHE_INV;
    cache->start                     		= (u8*)mpi->isp_init_param.isp_aaa_fifo2_base_vir + isp_cfa_aaa_ptr_pos;
    cache->size                      		= CFA_AAA_DATA_BLOCK;
    SGKS_MDI_DRV_IOCTL(SGKS_MDI_OPERATION_OPT_D_CACHE, 0, sizeof(sgks_mdi_opt_d_cache_s), SGKS_ERR_SYS_DRIVERIOCTLERR);
#endif

    memset(&pCfa->histogram_stat, 0, sizeof(pCfa->histogram_stat));

    if(!mpi->vin_global_info.high_mega_pixel_enable)
    {
        pCfa->tile_info.total_slices_x = 1;
        memcpy(aaa_info.cfa_statis, pCfa, CFA_AAA_DATA_BLOCK);
    }
    else
    {

        Printf("pCfa update!!\n");

        if (pCfa->tile_info.slice_index_x > 1)
        {
            Printf("slice index err!\n");
            return SGKS_ERR_ISP_STATISTICS_FAIL;
        }

        memcpy(aaa_info.cfa_statis + CFA_AAA_DATA_BLOCK * pCfa->tile_info.slice_index_x, pCfa, CFA_AAA_DATA_BLOCK);

        pCfa = (struct cfa_aaa_stat_s *)((u8 *)pCfa + CFA_AAA_DATA_BLOCK);

        if(((void *)pCfa) > (aaa_fifo2_base + CFA_AAA_DATA_BLOCK_SIZE - 1))
        {
            pCfa = (struct cfa_aaa_stat_s *)aaa_fifo2_base;
        }

        if(pCfa->tile_info.slice_index_x > 1)
        {
            Printf("slice index err!\n");
            return SGKS_ERR_ISP_STATISTICS_FAIL;
        }

        memcpy(aaa_info.cfa_statis + CFA_AAA_DATA_BLOCK * pCfa->tile_info.slice_index_x, pCfa, CFA_AAA_DATA_BLOCK);

    }

#if 0
    Printf("AWB %d %d %d %d %d\n", pCfa->tile_info.awb_tile_num_col, pCfa->tile_info.awb_tile_num_row, pCfa->tile_info.awb_tile_width, pCfa->tile_info.awb_tile_height, pCfa->tile_info.awb_rgb_shift);
    Printf("AE %d %d %d %d %d\n", pCfa->tile_info.ae_tile_num_col, pCfa->tile_info.ae_tile_num_row, pCfa->tile_info.ae_tile_width, pCfa->tile_info.ae_tile_height, pCfa->tile_info.ae_linear_y_shift);
    Printf("%dslice %d %d %d %d\n", pCfa->tile_info.slice_index_x, pCfa->tile_info.ae_tile_num_col, pCfa->tile_info.ae_tile_num_row, pCfa->tile_info.awb_tile_num_row, pCfa->tile_info.awb_tile_num_col);
    Printf("awb %d %d %d %d %d\n", pCfa->awb_stat[511].sum_r, pCfa->awb_stat[511].sum_g, pCfa->awb_stat[511].sum_b, pCfa->awb_stat[511].count_min, pCfa->awb_stat[511].count_max);

    Printf("CFA Hist bin [32] %d %d %d %d.\n"
           ,pCfa->histogram_stat.his_bin_y[32]
           ,pCfa->histogram_stat.his_bin_r[32]
           ,pCfa->histogram_stat.his_bin_g[32]
           ,pCfa->histogram_stat.his_bin_b[32]);
#endif
    *(aaa_info.cfa_data_valid) = 1;


    return SGKS_SUCCESS;

}

static int sgks_isp_cmd_3AStatisticsSetup(sgks_mpi_manager_s *mpi, SGKS_AAA_STATISTICS_CONFIG_S *param)
{
    sgks_dsp_cmd_aaa_statistics_setup_s    dsp_cmd;
    SGKS_AAA_STATISTICS_CONFIG_S    mw_cmd;


    if(sgks_isp_CheckFwStatus() < 0)
    {
        return SGKS_FAIL;
    }


    memcpy(&mw_cmd, param, sizeof(mw_cmd));


    if(mpi->isp_init_param.isp_aaa_fifo1_base_phy == NULL || mpi->isp_init_param.isp_aaa_fifo2_base_phy == NULL)
    {
        return SGKS_FAIL;
    }

    memset(&dsp_cmd, 0, sizeof(dsp_cmd));
    dsp_cmd.cmd_code 								= SGKS_DSP_CMD_AAA_STATISTICS_SETUP;
    dsp_cmd.on 										= mw_cmd.enable;
    dsp_cmd.auto_shift 								= mw_cmd.auto_shift;
    dsp_cmd.data_fifo_base 							= mpi->isp_init_param.isp_aaa_fifo1_base_phy;
    dsp_cmd.data_fifo_limit 						= (u8*)mpi->isp_init_param.isp_aaa_fifo1_base_phy + RGB_AAA_DATA_BLOCK_SIZE - 1;
    dsp_cmd.data_fifo2_base 						= mpi->isp_init_param.isp_aaa_fifo2_base_phy;
    dsp_cmd.data_fifo2_limit 						= (u8*)mpi->isp_init_param.isp_aaa_fifo2_base_phy + CFA_AAA_DATA_BLOCK_SIZE - 1;
    dsp_cmd.awb_tile_num_col 						= mw_cmd.awb_tile_num_col;
    dsp_cmd.awb_tile_num_row 						= mw_cmd.awb_tile_num_row;
    dsp_cmd.awb_tile_col_start 						= mw_cmd.awb_tile_col_start;
    dsp_cmd.awb_tile_row_start 						= mw_cmd.awb_tile_row_start;
    dsp_cmd.awb_tile_width 							= mw_cmd.awb_tile_width;
    dsp_cmd.awb_tile_height 						= mw_cmd.awb_tile_height;
    dsp_cmd.awb_tile_active_width 					= mw_cmd.awb_tile_active_width;
    dsp_cmd.awb_tile_active_height 					= mw_cmd.awb_tile_active_height;
    dsp_cmd.awb_pix_min_value 						= mw_cmd.awb_pix_min_value;
    dsp_cmd.awb_pix_max_value 						= mw_cmd.awb_pix_max_value;
    dsp_cmd.ae_tile_num_col 						= mw_cmd.ae_tile_num_col;
    dsp_cmd.ae_tile_num_row 						= mw_cmd.ae_tile_num_row;
    dsp_cmd.ae_tile_col_start 						= mw_cmd.ae_tile_col_start;
    dsp_cmd.ae_tile_row_start 						= mw_cmd.ae_tile_row_start;
    dsp_cmd.ae_tile_width 							= mw_cmd.ae_tile_width;
    dsp_cmd.ae_tile_height 							= mw_cmd.ae_tile_height;
    dsp_cmd.af_tile_num_col 						= mw_cmd.af_tile_num_col;
    dsp_cmd.af_tile_num_row 						= mw_cmd.af_tile_num_row;
    dsp_cmd.af_tile_col_start 						= mw_cmd.af_tile_col_start;
    dsp_cmd.af_tile_row_start 						= mw_cmd.af_tile_row_start;
    dsp_cmd.af_tile_width 							= mw_cmd.af_tile_width;
    dsp_cmd.af_tile_height 							= mw_cmd.af_tile_height;
    dsp_cmd.af_tile_active_width 					= mw_cmd.af_tile_active_width;
    dsp_cmd.af_tile_active_height 					= mw_cmd.af_tile_active_height;
    dsp_cmd.ae_pix_min_value 						= mw_cmd.ae_pix_min_value;
    dsp_cmd.ae_pix_max_value 						= mw_cmd.ae_pix_max_value;


    int ret = sgks_mpi_cmd_Send(&dsp_cmd, sizeof(dsp_cmd));
    if (ret != SGKS_SUCCESS)
    {
        return ret;
    }

    return SGKS_SUCCESS;


}


/**
 * AAA Statistic debug
 */
static int sgks_isp_cmd_3AStatisticsEx(sgks_mpi_manager_s *mpi, struct aaa_statistics_ex *param)
{
    sgks_dsp_cmd_aaa_statistics_setup1_s dsp_cmd1;
    sgks_dsp_cmd_aaa_statistics_setup2_s dsp_cmd2;
    struct aaa_statistics_ex mw_cmd;
    u32 i;
    if(sgks_isp_CheckFwStatus() < 0)
    {
        return SGKS_FAIL;
    }

    memcpy(&mw_cmd, param, sizeof(mw_cmd));

    memset(&dsp_cmd1, 0, sizeof(dsp_cmd1));
    memset(&dsp_cmd2, 0, sizeof(dsp_cmd2));


    dsp_cmd1.cmd_code 							= SGKS_DSP_CMD_AAA_STATISTICS_SETUP1;
    dsp_cmd1.af_horizontal_filter1_mode			= mw_cmd.af_horizontal_filter1_mode;
    dsp_cmd1.af_horizontal_filter1_stage1_enb 	= mw_cmd.af_horizontal_filter1_stage1_enb;
    dsp_cmd1.af_horizontal_filter1_stage2_enb 	= mw_cmd.af_horizontal_filter1_stage2_enb;
    dsp_cmd1.af_horizontal_filter1_stage3_enb 	= mw_cmd.af_horizontal_filter1_stage3_enb;

    for(i = 0; i < 7; i++)
    {
        dsp_cmd1.af_horizontal_filter1_gain[i] = mw_cmd.af_horizontal_filter1_gain[i];
    }

    for(i = 0; i < 4; i++)
    {
        dsp_cmd1.af_horizontal_filter1_shift[i] = mw_cmd.af_horizontal_filter1_shift[i];
    }

    dsp_cmd1.af_horizontal_filter1_bias_off 	= mw_cmd.af_horizontal_filter1_bias_off;
    dsp_cmd1.af_vertical_filter1_thresh 		= mw_cmd.af_vertical_filter1_thresh;
    dsp_cmd1.af_tile_fv1_horizontal_shift 		= mw_cmd.af_tile_fv1_horizontal_shift;
    dsp_cmd1.af_tile_fv1_vertical_shift 		= mw_cmd.af_tile_fv1_vertical_shift;
    dsp_cmd1.af_tile_fv1_horizontal_weight 		= mw_cmd.af_tile_fv1_horizontal_weight;
    dsp_cmd1.af_tile_fv1_vertical_weight 		= mw_cmd.af_tile_fv1_vertical_weight;

    int ret = sgks_mpi_cmd_Send(&dsp_cmd1, sizeof(dsp_cmd1));
    if (ret != SGKS_SUCCESS)
    {
        return ret;
    }



    dsp_cmd2.cmd_code 							= SGKS_DSP_CMD_AAA_STATISTICS_SETUP2;
    dsp_cmd2.af_horizontal_filter2_mode 		= mw_cmd.af_horizontal_filter2_mode;
    dsp_cmd2.af_horizontal_filter2_stage1_enb 	= mw_cmd.af_horizontal_filter2_stage1_enb;
    dsp_cmd2.af_horizontal_filter2_stage2_enb 	= mw_cmd.af_horizontal_filter2_stage2_enb;
    dsp_cmd2.af_horizontal_filter2_stage3_enb 	= mw_cmd.af_horizontal_filter2_stage3_enb;

    for(i = 0; i < 7; i ++)
    {
        dsp_cmd2.af_horizontal_filter2_gain[i] 	= mw_cmd.af_horizontal_filter2_gain[i];
    }

    for(i = 0; i < 4; i++)
    {
        dsp_cmd2.af_horizontal_filter2_shift[i] = mw_cmd.af_horizontal_filter2_shift[i];
    }

    dsp_cmd2.af_horizontal_filter2_bias_off 	= mw_cmd.af_horizontal_filter2_bias_off;
    dsp_cmd2.af_vertical_filter2_thresh 		= mw_cmd.af_vertical_filter2_thresh;
    dsp_cmd2.af_tile_fv2_horizontal_shift 		= mw_cmd.af_tile_fv2_horizontal_shift;
    dsp_cmd2.af_tile_fv2_vertical_shift 		= mw_cmd.af_tile_fv2_vertical_shift;
    dsp_cmd2.af_tile_fv2_horizontal_weight 		= mw_cmd.af_tile_fv2_horizontal_weight;
    dsp_cmd2.af_tile_fv2_vertical_weight 		= mw_cmd.af_tile_fv2_vertical_weight;

    ret = sgks_mpi_cmd_Send(&dsp_cmd2, sizeof(dsp_cmd2));
    if (ret != SGKS_SUCCESS)
    {
        return ret;
    }


    return SGKS_SUCCESS;


}

static int sgks_isp_cmd_Mctf_MV_StabilizerSetup(sgks_mpi_manager_s *mpi, struct mctf_mv_stab_info *param)
{
    sgks_dsp_cmd_mctf_mv_stab_setup_s dsp_cmd;
    struct mctf_mv_stab_info mw_cmd;

    if(sgks_isp_CheckFwStatus() < 0)
    {
        return SGKS_FAIL;
    }

    memcpy(&mw_cmd, param, sizeof(mw_cmd));


    memset(&dsp_cmd, 0, sizeof(dsp_cmd));
    dsp_cmd.cmd_code 						= SGKS_DSP_CMD_MCTF_MV_STAB_SETUP;
    dsp_cmd.noise_filter_strength 			= mw_cmd.noise_filter_strength;
    dsp_cmd.image_stabilize_strength 		= mw_cmd.image_stabilize_strength;
    dsp_cmd.still_noise_filter_strength 	= mw_cmd.still_noise_filter_strength;
    dsp_cmd.mctf_cfg_dram_addr 				= mpi->isp_init_param.isp_mctf_cfg_addr_phy;


    memcpy(mpi->isp_init_param.isp_mctf_cfg_addr_vir, (void *)mw_cmd.mctf_cfg_dram_addr, MCTF_CFG_SIZE);
    //sgks_isp_clean_cache(mctf_cfg_addr, MCTF_CFG_SIZE);

    u8 *msg_buf = NULL;
    SGKS_MDI_DRV_BUFF(msg_buf);
    sgks_mdi_opt_d_cache_s *cache 	 = msg_buf;
    cache->cache_type                = SGKS_OPT_D_CACHE_CLAEN;
    cache->start                     = mpi->isp_init_param.isp_mctf_cfg_addr_phy;
    cache->size                      = MCTF_CFG_SIZE;
    SGKS_MDI_DRV_IOCTL(SGKS_MDI_OPERATION_OPT_D_CACHE, 0, sizeof(sgks_mdi_opt_d_cache_s), SGKS_ERR_SYS_DRIVERIOCTLERR);

    int ret = sgks_mpi_cmd_Send(&dsp_cmd, sizeof(dsp_cmd));
    if (ret != SGKS_SUCCESS)
    {
        return ret;
    }


    return SGKS_SUCCESS;

}

static int sgks_isp_cmd_MctfGmvSetup(sgks_mpi_manager_s *mpi, struct mctf_gmv_info *param)
{
    sgks_dsp_cmd_mctf_gmv_setup_s    dsp_cmd;
    struct mctf_gmv_info    mw_cmd;
    if(sgks_isp_CheckFwStatus() < 0)
    {
        return SGKS_FAIL;
    }

    memcpy(&mw_cmd, param, sizeof(mw_cmd));

    memset(&dsp_cmd, 0, sizeof(dsp_cmd));
    dsp_cmd.cmd_code 					= SGKS_DSP_CMD_MCTF_GMV_SETUP;
    dsp_cmd.enable_external_gmv 		= mw_cmd.enable_external_gmv;
    dsp_cmd.external_gmv 				= mw_cmd.external_gmv;

    int ret = sgks_mpi_cmd_Send(&dsp_cmd, sizeof(dsp_cmd));
    if (ret != SGKS_SUCCESS)
    {
        return ret;
    }


    return SGKS_SUCCESS;

}

static int sgks_isp_cmd_NoiseFilterSetup(sgks_mpi_manager_s *mpi, u32 strength)
{
    sgks_dsp_cmd_noise_filter_setup_s dsp_cmd;

    if(sgks_isp_CheckFwStatus() < 0)
    {
        return SGKS_FAIL;
    }
    memset(&dsp_cmd, 0, sizeof(dsp_cmd));
    dsp_cmd.cmd_code = SGKS_DSP_CMD_NOISE_FILTER_SETUP;
    dsp_cmd.strength = strength;

    int ret = sgks_mpi_cmd_Send(&dsp_cmd, sizeof(dsp_cmd));
    if (ret != SGKS_SUCCESS)
    {
        return ret;
    }


    return SGKS_SUCCESS;


}

static int sgks_isp_cmd_BlackLevelGlobalOffset(sgks_mpi_manager_s *mpi, struct black_level_global_offset *param)
{
    sgks_dsp_cmd_black_level_global_offset_s    dsp_cmd;
    struct black_level_global_offset mw_cmd;

    if(sgks_isp_CheckFwStatus() < 0)
    {
        return SGKS_FAIL;
    }

    memcpy(&mw_cmd, param, sizeof(mw_cmd));


    memset(&dsp_cmd, 0, sizeof(dsp_cmd));
    dsp_cmd.cmd_code 					= SGKS_DSP_CMD_BLACK_LEVEL_GLOBAL_OFFSET;
    dsp_cmd.global_offset_ee 			= mw_cmd.global_offset_ee;
    dsp_cmd.global_offset_eo 			= mw_cmd.global_offset_eo;
    dsp_cmd.global_offset_oe 			= mw_cmd.global_offset_oe;
    dsp_cmd.global_offset_oo 			= mw_cmd.global_offset_oo;
    dsp_cmd.black_level_offset_red 		= mw_cmd.black_level_offset_red;
    dsp_cmd.black_level_offset_green 	= mw_cmd.black_level_offset_green;
    dsp_cmd.black_level_offset_blue 	= mw_cmd.black_level_offset_blue;

    int ret = sgks_mpi_cmd_Send(&dsp_cmd, sizeof(dsp_cmd));
    if (ret != SGKS_SUCCESS)
    {
        return ret;
    }


    return SGKS_SUCCESS;

}

static int sgks_isp_cmd_CfaDomainLeakageFilterSetup(sgks_mpi_manager_s *mpi, struct cfa_leakage_filter_info *param)
{
    sgks_dsp_cmd_cfa_domain_leakage_filter_s dsp_cmd;
    struct cfa_leakage_filter_info mw_cmd;

    if(sgks_isp_CheckFwStatus() < 0)
    {
        return SGKS_FAIL;
    }

    memcpy(&mw_cmd, param, sizeof(mw_cmd));


    memset(&dsp_cmd, 0, sizeof(dsp_cmd));
    dsp_cmd.cmd_code 					= SGKS_DSP_CMD_CFA_DOMAIN_LEAKAGE_FILTER;
    dsp_cmd.enable 						= mw_cmd.enable;
    dsp_cmd.alpha_rr 					= mw_cmd.alpha_rr;
    dsp_cmd.alpha_rb 					= mw_cmd.alpha_rb;
    dsp_cmd.alpha_br 					= mw_cmd.alpha_br;
    dsp_cmd.alpha_bb 					= mw_cmd.alpha_bb;
    dsp_cmd.saturation_level 			= mw_cmd.saturation_level;

    int ret = sgks_mpi_cmd_Send(&dsp_cmd, sizeof(dsp_cmd));
    if (ret != SGKS_SUCCESS)
    {
        return ret;
    }


    return SGKS_SUCCESS;

}

static int sgks_isp_cmd_CfaNoiseFilter(sgks_mpi_manager_s *mpi, cfa_noise_filter_info_t *param)
{
    sgks_dsp_cmd_cfa_noise_filter_info_s dsp_cmd;
    cfa_noise_filter_info_t mw_cmd;

    if(sgks_isp_CheckFwStatus() < 0)
    {
        return SGKS_FAIL;
    }

    memcpy(&mw_cmd, param, sizeof(mw_cmd));

    memset(&dsp_cmd, 0, sizeof(dsp_cmd));
    dsp_cmd.cmd_code 								= SGKS_DSP_CMD_CFA_NOISE_FILTER_INFO;
    dsp_cmd.enable 									= mw_cmd.enable;
    dsp_cmd.center_weight_red 						= mw_cmd.iso_center_weight_red;
    dsp_cmd.center_weight_green 					= mw_cmd.iso_center_weight_green;
    dsp_cmd.center_weight_blue 						= mw_cmd.iso_center_weight_blue;
    dsp_cmd.thresh_k0_red 							= mw_cmd.iso_thresh_k0_red;
    dsp_cmd.thresh_k0_green 						= mw_cmd.iso_thresh_k0_green;
    dsp_cmd.thresh_k0_blue 							= mw_cmd.iso_thresh_k0_blue;
    dsp_cmd.thresh_k0_close 						= mw_cmd.iso_thresh_k0_close;
    dsp_cmd.thresh_k1_red 							= mw_cmd.iso_thresh_k1_red;
    dsp_cmd.thresh_k1_green 						= mw_cmd.iso_thresh_k1_green;
    dsp_cmd.thresh_k1_blue 							= mw_cmd.iso_thresh_k1_blue;
    dsp_cmd.direct_center_weight_red 				= mw_cmd.direct_center_weight_red;
    dsp_cmd.direct_center_weight_green 				= mw_cmd.direct_center_weight_green;
    dsp_cmd.direct_center_weight_blue 				= mw_cmd.direct_center_weight_blue;
    dsp_cmd.direct_thresh_k0_red 					= mw_cmd.direct_thresh_k0_red;
    dsp_cmd.direct_thresh_k0_green 					= mw_cmd.direct_thresh_k0_green;
    dsp_cmd.direct_thresh_k0_blue 					= mw_cmd.direct_thresh_k0_blue;
    dsp_cmd.direct_thresh_k1_red 					= mw_cmd.direct_thresh_k1_red;
    dsp_cmd.direct_thresh_k1_green 					= mw_cmd.direct_thresh_k1_green;
    dsp_cmd.direct_thresh_k1_blue 					= mw_cmd.direct_thresh_k1_blue;
    dsp_cmd.direct_grad_thresh 						= mw_cmd.direct_grad_thresh;

    int ret = sgks_mpi_cmd_Send(&dsp_cmd, sizeof(dsp_cmd));
    if (ret != SGKS_SUCCESS)
    {
        return ret;
    }


    return SGKS_SUCCESS;
}

static int sgks_isp_cmd_VignetteCompensation(sgks_mpi_manager_s *mpi, struct vignette_compensation_info *param)
{
    sgks_dsp_cmd_vignette_compensation_s dsp_cmd;
    struct vignette_compensation_info mw_cmd;

    if(sgks_isp_CheckFwStatus() < 0)
    {
        return SGKS_FAIL;
    }

    memcpy(&mw_cmd, param, sizeof(mw_cmd));


    memset(&dsp_cmd, 0, sizeof(dsp_cmd));
    dsp_cmd.cmd_code 					= SGKS_DSP_CMD_VIGNETTE_COMPENSATION;
    dsp_cmd.enable 						= mw_cmd.enable;
    dsp_cmd.gain_shift 					= mw_cmd.gain_shift;

    memcpy(mpi->isp_init_param.isp_vignette_r_gain_vir, (void *)mw_cmd.vignette_red_gain_ptr, MAX_VIGNETTE_NUM);
    memcpy(mpi->isp_init_param.isp_vignette_ge_gain_vir, (void *)mw_cmd.vignette_green_even_gain_ptr, MAX_VIGNETTE_NUM);
    memcpy(mpi->isp_init_param.isp_vignette_go_gain_vir, (void *)mw_cmd.vignette_green_odd_gain_ptr, MAX_VIGNETTE_NUM);
    memcpy(mpi->isp_init_param.isp_vignette_b_gain_vir, (void *)mw_cmd.vignette_blue_gain_ptr, MAX_VIGNETTE_NUM);

    dsp_cmd.tile_gain_addr 				= mpi->isp_init_param.isp_vignette_r_gain_phy;
    dsp_cmd.tile_gain_addr_green_even 	= mpi->isp_init_param.isp_vignette_ge_gain_phy;
    dsp_cmd.tile_gain_addr_green_odd 	= mpi->isp_init_param.isp_vignette_go_gain_phy;
    dsp_cmd.tile_gain_addr_blue 		= mpi->isp_init_param.isp_vignette_b_gain_phy;



    u8 *msg_buf = NULL;
    SGKS_MDI_DRV_BUFF(msg_buf);
    sgks_mdi_opt_d_cache_s *cache 	 = msg_buf;
    cache->cache_type                = SGKS_OPT_D_CACHE_CLAEN;
    cache->start                     = mpi->isp_init_param.isp_vignette_r_gain_phy;
    cache->size                      = MAX_VIGNETTE_NUM * sizeof(u8);
    SGKS_MDI_DRV_IOCTL(SGKS_MDI_OPERATION_OPT_D_CACHE, 0, sizeof(sgks_mdi_opt_d_cache_s), SGKS_ERR_SYS_DRIVERIOCTLERR);


    SGKS_MDI_DRV_CLEAN_DATABUFF();
    cache 	 						 = msg_buf;
    cache->cache_type                = SGKS_OPT_D_CACHE_CLAEN;
    cache->start                     = mpi->isp_init_param.isp_vignette_ge_gain_phy;
    cache->size                      = MAX_VIGNETTE_NUM * sizeof(u8);
    SGKS_MDI_DRV_IOCTL(SGKS_MDI_OPERATION_OPT_D_CACHE, 0, sizeof(sgks_mdi_opt_d_cache_s), SGKS_ERR_SYS_DRIVERIOCTLERR);

    SGKS_MDI_DRV_CLEAN_DATABUFF();
    cache 	 						 = msg_buf;
    cache->cache_type                = SGKS_OPT_D_CACHE_CLAEN;
    cache->start                     = mpi->isp_init_param.isp_vignette_go_gain_phy;
    cache->size                      = MAX_VIGNETTE_NUM * sizeof(u8);
    SGKS_MDI_DRV_IOCTL(SGKS_MDI_OPERATION_OPT_D_CACHE, 0, sizeof(sgks_mdi_opt_d_cache_s), SGKS_ERR_SYS_DRIVERIOCTLERR);

    SGKS_MDI_DRV_CLEAN_DATABUFF();
    cache 	 						 = msg_buf;
    cache->cache_type                = SGKS_OPT_D_CACHE_CLAEN;
    cache->start                     = mpi->isp_init_param.isp_vignette_b_gain_phy;
    cache->size                      = MAX_VIGNETTE_NUM * sizeof(u8);
    SGKS_MDI_DRV_IOCTL(SGKS_MDI_OPERATION_OPT_D_CACHE, 0, sizeof(sgks_mdi_opt_d_cache_s), SGKS_ERR_SYS_DRIVERIOCTLERR);


    //sgks_isp_clean_cache(vignette_r_gain, MAX_VIGNETTE_NUM * sizeof(u8));
    //sgks_isp_clean_cache(vignette_ge_gain, MAX_VIGNETTE_NUM * sizeof(u8));
    //sgks_isp_clean_cache(vignette_go_gain, MAX_VIGNETTE_NUM * sizeof(u8));
    //sgks_isp_clean_cache(vignette_b_gain, MAX_VIGNETTE_NUM * sizeof(u8));

    int ret = sgks_mpi_cmd_Send(&dsp_cmd, sizeof(dsp_cmd));
    if (ret != SGKS_SUCCESS)
    {
        return ret;
    }


    return SGKS_SUCCESS;

}

static int sgks_isp_cmd_LocalExposure(sgks_mpi_manager_s *mpi, struct local_exposure_info *param)
{
    sgks_dsp_cmd_local_exposure_s dsp_cmd;
    struct local_exposure_info mw_cmd;

    if(sgks_isp_CheckFwStatus() < 0)
    {
        return SGKS_FAIL;
    }

    memcpy(&mw_cmd, param, sizeof(mw_cmd));

    memset(&dsp_cmd, 0, sizeof(dsp_cmd));
    memcpy(mpi->isp_init_param.isp_exposure_gain_curve_vir, mw_cmd.gain_curve_table, NUM_EXPOSURE_CURVE * sizeof(u16));
    dsp_cmd.cmd_code 							= SGKS_DSP_CMD_LOCAL_EXPOSURE;
    dsp_cmd.enable 								= mw_cmd.enable;
    dsp_cmd.radius 								= mw_cmd.radius;
    dsp_cmd.luma_weight_red 					= mw_cmd.luma_weight_red;
    dsp_cmd.luma_weight_green					= mw_cmd.luma_weight_green;
    dsp_cmd.luma_weight_blue 					= mw_cmd.luma_weight_blue;
    dsp_cmd.luma_weight_sum_shift 				= mw_cmd.luma_weight_sum_shift;
    dsp_cmd.gain_curve_table_addr 				= mpi->isp_init_param.isp_exposure_gain_curve_phy;
    dsp_cmd.black_level_offset_red 				= mw_cmd.black_level_offset_red;
    dsp_cmd.black_level_offset_green 			= mw_cmd.black_level_offset_green;
    dsp_cmd.black_level_offset_blue 			= mw_cmd.black_level_offset_blue;
    dsp_cmd.luma_offset 						= mw_cmd.luma_offset;
    dsp_cmd.global_offset 						= mw_cmd.global_offset;

    //sgks_isp_clean_cache(exposure_gain_curve, NUM_EXPOSURE_CURVE * sizeof(u16));
    u8 *msg_buf = NULL;
    SGKS_MDI_DRV_BUFF(msg_buf);
    sgks_mdi_opt_d_cache_s *cache		= msg_buf;
    cache->cache_type                	= SGKS_OPT_D_CACHE_CLAEN;
    cache->start                     	= mpi->isp_init_param.isp_exposure_gain_curve_phy;
    cache->size                      	= NUM_EXPOSURE_CURVE * sizeof(u16);
    SGKS_MDI_DRV_IOCTL(SGKS_MDI_OPERATION_OPT_D_CACHE, 0, sizeof(sgks_mdi_opt_d_cache_s), SGKS_ERR_SYS_DRIVERIOCTLERR);

    int ret = sgks_mpi_cmd_Send(&dsp_cmd, sizeof(dsp_cmd));
    if (ret != SGKS_SUCCESS)
    {
        return ret;
    }


    return SGKS_SUCCESS;

}

static int sgks_isp_cmd_ColorCorrection(sgks_mpi_manager_s *mpi, struct color_correction_info *param)
{
    sgks_dsp_cmd_color_correction_s dsp_cmd;
    struct color_correction_info mw_cmd;

    if(sgks_isp_CheckFwStatus() < 0)
    {
        return SGKS_FAIL;
    }

    memcpy(&mw_cmd, param, sizeof(mw_cmd));


    memset(&dsp_cmd, 0, sizeof(dsp_cmd));
    dsp_cmd.cmd_code 						= SGKS_DSP_CMD_COLOR_CORRECTION;
    dsp_cmd.enable 							= mw_cmd.enable;
    dsp_cmd.no_interpolation 				= mw_cmd.no_interpolation;
    dsp_cmd.yuv422_foramt 					= mw_cmd.yuv422_foramt;
    dsp_cmd.uv_center 						= mw_cmd.uv_center;
    dsp_cmd.multi_red 						= mw_cmd.multi_red;
    dsp_cmd.multi_green 					= mw_cmd.multi_green;
    dsp_cmd.multi_blue 						= mw_cmd.multi_blue;
    dsp_cmd.in_lookup_table_addr 			= mpi->isp_init_param.isp_input_lookup_table_phy;
    dsp_cmd.matrix_addr 					= mpi->isp_init_param.isp_matrix_dram_address_phy;
    dsp_cmd.output_lookup_bypass 			= mw_cmd.output_lookup_bypass;

    if(dsp_cmd.output_lookup_bypass == 1)
    {
        dsp_cmd.out_lookup_table_addr = 0;
    }
    else
    {
        dsp_cmd.out_lookup_table_addr = mpi->isp_init_param.isp_output_lookup_table_phy;

        if(mw_cmd.out_lut_addr != NULL)
        {
            memcpy(mpi->isp_init_param.isp_output_lookup_table_vir, mw_cmd.out_lut_addr, NUM_OUT_LOOKUP * sizeof(u32));
        }
    }

    if(mw_cmd.in_lut_addr != NULL)
    {
        memcpy(mpi->isp_init_param.isp_input_lookup_table_vir, mw_cmd.in_lut_addr, NUM_IN_LOOKUP * sizeof(u32));
    }

    if(mw_cmd.matrix_addr != NULL)
    {
        memcpy(mpi->isp_init_param.isp_matrix_dram_address_vir, mw_cmd.matrix_addr, NUM_MATRIX * sizeof(u32));
    }

    dsp_cmd.group_index = mw_cmd.group_index;

    u8 *msg_buf = NULL;
    SGKS_MDI_DRV_BUFF(msg_buf);
    sgks_mdi_opt_d_cache_s *cache 		= msg_buf;
    cache->cache_type                	= SGKS_OPT_D_CACHE_CLAEN;
    cache->start                     	= mpi->isp_init_param.isp_input_lookup_table_phy;
    cache->size                      	= NUM_IN_LOOKUP * sizeof(u32);
    SGKS_MDI_DRV_IOCTL(SGKS_MDI_OPERATION_OPT_D_CACHE, 0, sizeof(sgks_mdi_opt_d_cache_s), SGKS_ERR_SYS_DRIVERIOCTLERR);

    SGKS_MDI_DRV_CLEAN_DATABUFF();
    cache 	 						 = msg_buf;
    cache->cache_type                = SGKS_OPT_D_CACHE_CLAEN;
    cache->start                     = mpi->isp_init_param.isp_matrix_dram_address_phy;
    cache->size                      = NUM_MATRIX * sizeof(u32);
    SGKS_MDI_DRV_IOCTL(SGKS_MDI_OPERATION_OPT_D_CACHE, 0, sizeof(sgks_mdi_opt_d_cache_s), SGKS_ERR_SYS_DRIVERIOCTLERR);

    //sgks_isp_clean_cache(input_lookup_table, NUM_IN_LOOKUP * sizeof(u32));
    //sgks_isp_clean_cache(matrix_dram_address, NUM_MATRIX * sizeof(u32));

    if(mw_cmd.out_lut_addr != NULL)
    {
        //sgks_isp_clean_cache(output_lookup_table, NUM_OUT_LOOKUP * sizeof(u32));
        SGKS_MDI_DRV_CLEAN_DATABUFF();
        cache 	 						 = msg_buf;
        cache->cache_type                = SGKS_OPT_D_CACHE_CLAEN;
        cache->start                     = mpi->isp_init_param.isp_output_lookup_table_phy;
        cache->size                      = NUM_OUT_LOOKUP * sizeof(u32);
        SGKS_MDI_DRV_IOCTL(SGKS_MDI_OPERATION_OPT_D_CACHE, 0, sizeof(sgks_mdi_opt_d_cache_s), SGKS_ERR_SYS_DRIVERIOCTLERR);
    }


    int ret = sgks_mpi_cmd_Send(&dsp_cmd, sizeof(dsp_cmd));
    if (ret != SGKS_SUCCESS)
    {
        return ret;
    }

    return SGKS_SUCCESS;

}

static int sgks_isp_cmd_RgbToYuvSetup(sgks_mpi_manager_s *mpi, struct rgb_to_yuv_info *param)
{
    sgks_dsp_cmd_rgb_to_yuv_setup_s dsp_cmd;
    struct rgb_to_yuv_info mw_cmd;

    if(sgks_isp_CheckFwStatus() < 0)
    {
        return SGKS_FAIL;
    }

    memcpy(&mw_cmd, param, sizeof(mw_cmd));

    memset(&dsp_cmd, 0, sizeof(dsp_cmd));
    dsp_cmd.cmd_code 				= SGKS_DSP_CMD_RGB_TO_YUV_SETUP;
    memcpy(dsp_cmd.matrix_values, mw_cmd.matrix_values, RGB_TO_YUV_MATRIX_SIZE * sizeof(u16));
    dsp_cmd.y_offset 				= mw_cmd.y_offset;
    dsp_cmd.u_offset 				= mw_cmd.u_offset;
    dsp_cmd.v_offset 				= mw_cmd.v_offset;
    dsp_cmd.group_index 			= mw_cmd.group_index;


    int ret = sgks_mpi_cmd_Send(&dsp_cmd, sizeof(dsp_cmd));
    if (ret != SGKS_SUCCESS)
    {
        return ret;
    }

    return SGKS_SUCCESS;


}

static int sgks_isp_cmd_ChromaScale(sgks_mpi_manager_s *mpi, struct chroma_scale_info *param)
{
    sgks_dsp_cmd_chroma_scale_s dsp_cmd;
    struct chroma_scale_info mw_cmd;

    if(sgks_isp_CheckFwStatus() < 0)
    {
        return SGKS_FAIL;
    }
    memcpy(&mw_cmd, param, sizeof(mw_cmd));

    memset(&dsp_cmd, 0, sizeof(dsp_cmd));
    dsp_cmd.cmd_code 						= SGKS_DSP_CMD_CHROMA_SCALE;
    dsp_cmd.enable 							= mw_cmd.enable;
    dsp_cmd.make_legal 						= mw_cmd.make_legal;
    dsp_cmd.u_weight_0 						= mw_cmd.u_weight_0;
    dsp_cmd.u_weight_1 						= mw_cmd.u_weight_1;
    dsp_cmd.u_weight_2 						= mw_cmd.u_weight_2;
    dsp_cmd.v_weight_0 						= mw_cmd.v_weight_0;
    dsp_cmd.v_weight_1 						= mw_cmd.v_weight_1;
    dsp_cmd.v_weight_2 						= mw_cmd.v_weight_2;
    dsp_cmd.gain_curver_addr 				= mpi->isp_init_param.isp_chroma_gain_curve_phy;
    memcpy(mpi->isp_init_param.isp_chroma_gain_curve_vir, mw_cmd.gain_curve, NUM_CHROMA_GAIN_CURVE * sizeof(u16));

    //sgks_isp_clean_cache(chroma_gain_curve, NUM_CHROMA_GAIN_CURVE * sizeof(u16));

    dsp_cmd.group_index 					= mw_cmd.group_index;

    int ret = sgks_mpi_cmd_Send(&dsp_cmd, sizeof(dsp_cmd));
    if (ret != SGKS_SUCCESS)
    {
        return ret;
    }

    return SGKS_SUCCESS;

}

static int sgks_isp_cmd_ChromaMedianFilter(sgks_mpi_manager_s *mpi, struct chroma_median_filter *param)
{
    sgks_dsp_cmd_chroma_median_filter_info_s dsp_cmd;
    struct chroma_median_filter mw_cmd;

    if(sgks_isp_CheckFwStatus() < 0)
    {
        return SGKS_FAIL;
    }

    memcpy(&mw_cmd, param, sizeof(mw_cmd));

    memset(&dsp_cmd, 0, sizeof(dsp_cmd));
    dsp_cmd.cmd_code 					= SGKS_DSP_CMD_CHROMA_MEDIAN_FILTER_INFO;
    dsp_cmd.enable 						= mw_cmd.enable;
    dsp_cmd.group_index 				= mw_cmd.group_index;
    dsp_cmd.k0123_table_addr 			= mpi->isp_init_param.isp_k0123_table_phy;
    dsp_cmd.u_sat_t0 					= mw_cmd.u_sat_t0;
    dsp_cmd.u_sat_t1 					= mw_cmd.u_sat_t1;
    dsp_cmd.v_sat_t0 					= mw_cmd.v_sat_t0;
    dsp_cmd.v_sat_t1 					= mw_cmd.v_sat_t1;
    dsp_cmd.u_act_t0					= mw_cmd.u_act_t0;
    dsp_cmd.u_act_t1 					= mw_cmd.u_act_t1;
    dsp_cmd.v_act_t0 					= mw_cmd.v_act_t0;
    dsp_cmd.v_act_t1 					= mw_cmd.v_act_t1;

    memcpy(mpi->isp_init_param.isp_k0123_table_vir, (void *)mw_cmd.k0123_table_addr, K0123_ARRAY_SIZE * sizeof(u16));

    //sgks_isp_clean_cache(k0123_table, K0123_ARRAY_SIZE * sizeof(u16));

    int ret = sgks_mpi_cmd_Send(&dsp_cmd, sizeof(dsp_cmd));
    if (ret != SGKS_SUCCESS)
    {
        return ret;
    }

    return SGKS_SUCCESS;

}

static int sgks_isp_cmd_LumaSharpening(sgks_mpi_manager_s *mpi, struct luma_sharpening_info *param)
{
    sgks_dsp_cmd_luma_sharpening_s dsp_cmd;
    struct luma_sharpening_info mw_cmd;

    if(sgks_isp_CheckFwStatus() < 0)
    {
        return SGKS_FAIL;
    }

    memcpy(&mw_cmd, param, sizeof(mw_cmd));

    memset(&dsp_cmd, 0, sizeof(dsp_cmd));
    dsp_cmd.cmd_code 				= SGKS_DSP_CMD_LUMA_SHARPENING;
    dsp_cmd.enable 					= mw_cmd.enable;
    dsp_cmd.grad_thresh_0 			= mw_cmd.grad_thresh_0;
    dsp_cmd.grad_thresh_1 			= mw_cmd.grad_thresh_1;
    dsp_cmd.smooth_shift 			= mw_cmd.smooth_shift;
    dsp_cmd.edge_shift 				= mw_cmd.edge_shift;
    dsp_cmd.edge_thresh 			= mw_cmd.edge_thresh;
    dsp_cmd.alpha_table_addr 		= mpi->isp_init_param.isp_luma_sharpening_alpha_table_phy;
    memcpy(mpi->isp_init_param.isp_luma_sharpening_alpha_table_vir, mw_cmd.alpha_table, NUM_ALPHA_TABLE);
    //sgks_isp_clean_cache(luma_sharpening_alpha_table, NUM_ALPHA_TABLE);
    dsp_cmd.group_index 			= mw_cmd.group_index;
    dsp_cmd.clip_low 				= mw_cmd.clip_low;
    dsp_cmd.clip_high 				= mw_cmd.clip_high;
    dsp_cmd.max_change_down 		= mw_cmd.max_change_down;
    dsp_cmd.max_change_up 			= mw_cmd.max_change_up;

    int ret = sgks_mpi_cmd_Send(&dsp_cmd, sizeof(dsp_cmd));
    if (ret != SGKS_SUCCESS)
    {
        return ret;
    }

    return SGKS_SUCCESS;

}

static int sgks_isp_cmd_LumaSharpFir(sgks_mpi_manager_s *mpi, struct luma_sharp_fir_info *param)
{
    sgks_dsp_cmd_luma_sharpening_FIR_config_s dsp_cmd;
    struct luma_sharp_fir_info mw_cmd;

    if(sgks_isp_CheckFwStatus() < 0)
    {
        return SGKS_FAIL;
    }

    memcpy(&mw_cmd, param, sizeof(mw_cmd));


    memset(&dsp_cmd, 0, sizeof(dsp_cmd));
    memcpy(mpi->isp_init_param.isp_coeff_fir1_addr_vir, (void *)mw_cmd.coeff_fir1_addr, 256);
    memcpy(mpi->isp_init_param.isp_coeff_fir2_addr_vir, (void *)mw_cmd.coeff_fir2_addr, 256);
    memcpy(mpi->isp_init_param.isp_coring_table_vir, (void *)mw_cmd.coring_table_addr, 256);

    dsp_cmd.cmd_code 					= SGKS_DSP_CMD_LUMA_SHARPENING_FIR_CONFIG;
    dsp_cmd.enable_FIR1 				= mw_cmd.enable_fir1;
    dsp_cmd.enable_FIR2 				= mw_cmd.enable_fir2;
    dsp_cmd.enable_FIR2_bypass_alpha 	= mw_cmd.enable_fir2_bypass_alpha;
    dsp_cmd.fir0_clip_low 				= mw_cmd.fir0_clip_low;
    dsp_cmd.fir0_clip_high 				= mw_cmd.fir0_clip_high;
    dsp_cmd.fir1_clip_low 				= mw_cmd.fir1_clip_low;
    dsp_cmd.fir1_clip_high 				= mw_cmd.fir1_clip_high;
    dsp_cmd.fir2_clip_low 				= mw_cmd.fir2_clip_low;
    dsp_cmd.fir2_clip_high 				= mw_cmd.fir2_clip_high;
    dsp_cmd.coeff_FIR0_addr 			= 0;
    dsp_cmd.coeff_FIR1_addr 			= mpi->isp_init_param.isp_coeff_fir1_addr_phy;
    dsp_cmd.coeff_FIR2_addr 			= mpi->isp_init_param.isp_coeff_fir2_addr_phy;
    dsp_cmd.coring_table_addr 			= mpi->isp_init_param.isp_coring_table_phy;
    dsp_cmd.group_index 				= mw_cmd.group_index;


    //sgks_isp_clean_cache((u8 *)coeff_fir1_addr, 256);
    //sgks_isp_clean_cache((u8 *)coeff_fir2_addr, 256);
    //sgks_isp_clean_cache((u8 *)coring_table, 256);

    int ret = sgks_mpi_cmd_Send(&dsp_cmd, sizeof(dsp_cmd));
    if (ret != SGKS_SUCCESS)
    {
        return ret;
    }

    return SGKS_SUCCESS;

}

static int sgks_isp_cmd_LumaSharpenEdgeCtrl(sgks_mpi_manager_s *mpi, struct luma_sharp_edge_info *param)
{
    sgks_dsp_cmd_luma_sharpening_edge_control_s    dsp_cmd;
    struct luma_sharp_edge_info    mw_cmd;

    if(sgks_isp_CheckFwStatus() < 0)
    {
        return SGKS_FAIL;
    }

    memcpy(&mw_cmd, param, sizeof(mw_cmd));

    memset(&dsp_cmd, 0, sizeof(dsp_cmd));
    dsp_cmd.cmd_code 					= SGKS_DSP_CMD_LUMA_SHARPENING_EDGE_CONTROL;
    dsp_cmd.group_index 				= mw_cmd.group_index;
    dsp_cmd.edge_threshold 				= mw_cmd.edge_thresh;
    dsp_cmd.edge_threshold_multiplier 	= mw_cmd.edge_thresh_multi;
    dsp_cmd.wide_weight 				= mw_cmd.wide_weight;
    dsp_cmd.narrow_weight 				= mw_cmd.narrow_weight;

    int ret = sgks_mpi_cmd_Send(&dsp_cmd, sizeof(dsp_cmd));
    if (ret != SGKS_SUCCESS)
    {
        return ret;
    }

    return SGKS_SUCCESS;

}

static int sgks_isp_cmd_LumaSharpenBlendCtrl(sgks_mpi_manager_s *mpi, struct luma_sharp_blend_info *param)
{
    sgks_dsp_cmd_luma_sharpening_blend_control_s    dsp_cmd;
    struct luma_sharp_blend_info    mw_cmd;

    if(sgks_isp_CheckFwStatus() < 0)
    {
        return SGKS_FAIL;
    }

    memcpy(&mw_cmd, param, sizeof(mw_cmd));

    memset(&dsp_cmd, 0, sizeof(dsp_cmd));
    dsp_cmd.cmd_code 						= SGKS_DSP_CMD_LUMA_SHARPENING_BLEND_CONTROL;
    dsp_cmd.group_index 					= mw_cmd.group_index;
    dsp_cmd.enable 							= mw_cmd.enable;
    dsp_cmd.edge_threshold_multiplier 		= mw_cmd.edge_thresh_multi;
    dsp_cmd.iso_threshold_multiplier 		= mw_cmd.iso_thresh_multi;
    dsp_cmd.edge_threshold0 				= mw_cmd.edge_thresh0;
    dsp_cmd.edge_threshold1 				= mw_cmd.edge_thresh1;
    dsp_cmd.dir_threshold0 					= mw_cmd.dir_thresh0;
    dsp_cmd.dir_threshold1 					= mw_cmd.dir_thresh1;
    dsp_cmd.iso_threshold0 					= mw_cmd.iso_thresh0;
    dsp_cmd.iso_threshold1 					= mw_cmd.iso_thresh1;

    int ret = sgks_mpi_cmd_Send(&dsp_cmd, sizeof(dsp_cmd));
    if (ret != SGKS_SUCCESS)
    {
        return ret;
    }

    return SGKS_SUCCESS;

}

static int sgks_isp_cmd_LumaSharpenLevelCtrl(sgks_mpi_manager_s *mpi, struct luma_sharp_level_info *param)
{
    sgks_dsp_cmd_luma_sharpening_level_control_s    dsp_cmd;
    struct luma_sharp_level_info    mw_cmd;

    if(sgks_isp_CheckFwStatus() < 0)
    {
        return SGKS_FAIL;
    }

    memcpy(&mw_cmd, param, sizeof(mw_cmd));

    memset(&dsp_cmd, 0, sizeof(dsp_cmd));
    dsp_cmd.cmd_code 						= SGKS_DSP_CMD_LUMA_SHARPENING_LEVEL_CONTROL;
    dsp_cmd.group_index 					= mw_cmd.group_index;
    dsp_cmd.select 							= mw_cmd.select;
    dsp_cmd.low 							= mw_cmd.low;
    dsp_cmd.low_0 							= mw_cmd.low_0;
    dsp_cmd.low_delta 						= mw_cmd.low_delta;
    dsp_cmd.low_val 						= mw_cmd.low_val;
    dsp_cmd.high 							= mw_cmd.high;
    dsp_cmd.high_0 							= mw_cmd.high_0;
    dsp_cmd.high_delta 						= mw_cmd.high_delta;
    dsp_cmd.high_val 						= mw_cmd.high_val;
    dsp_cmd.base_val 						= mw_cmd.base_val;
    dsp_cmd.area 							= mw_cmd.area;
    dsp_cmd.level_control_clip_low 			= mw_cmd.lvl_ctrl_clip_low;
    dsp_cmd.level_control_clip_low2 		= mw_cmd.lvl_ctrl_clip_low2;
    dsp_cmd.level_control_clip_high 		= mw_cmd.lvl_ctrl_clip_high;
    dsp_cmd.level_control_clip_high2 		= mw_cmd.lvl_ctrl_clip_high2;

    int ret = sgks_mpi_cmd_Send(&dsp_cmd, sizeof(dsp_cmd));
    if (ret != SGKS_SUCCESS)
    {
        return ret;
    }

    return SGKS_SUCCESS;

}

static int sgks_isp_cmd_LumaSharpenMiscCtrl(sgks_mpi_manager_s *mpi, struct luma_sharp_misc_info *param)
{
    sgks_dsp_cmd_luma_sharpening_misc_control_s    dsp_cmd;
    struct luma_sharp_misc_info    mw_cmd;

    if(sgks_isp_CheckFwStatus() < 0)
    {
        return SGKS_FAIL;
    }

    memcpy(&mw_cmd, param, sizeof(mw_cmd));


    memset(&dsp_cmd, 0, sizeof(dsp_cmd));
    dsp_cmd.cmd_code 					= SGKS_DSP_CMD_LUMA_SHARPENING_MISC_CONTROL;
    dsp_cmd.group_index 				= mw_cmd.group_index;
    dsp_cmd.coring_control 				= mw_cmd.coring_control;
    dsp_cmd.add_in_low_pass 			= mw_cmd.add_in_low_pass;
    dsp_cmd.second_input_enable 		= mw_cmd.second_input_enable;
    dsp_cmd.second_input_signed 		= mw_cmd.second_input_signed;
    dsp_cmd.second_input_shift 			= mw_cmd.second_input_shift;
    dsp_cmd.output_signed				= mw_cmd.output_signed;
    dsp_cmd.output_shift 				= mw_cmd.output_shift;
    dsp_cmd.abs 						= mw_cmd.abs;
    dsp_cmd.yuv 						= mw_cmd.yuv;

    int ret = sgks_mpi_cmd_Send(&dsp_cmd, sizeof(dsp_cmd));
    if (ret != SGKS_SUCCESS)
    {
        return ret;
    }

    return SGKS_SUCCESS;

}

static int sgks_isp_cmd_RgbGainAdjustment(sgks_mpi_manager_s *mpi, struct rgb_gain_info *param)
{
    sgks_dsp_cmd_rgb_gain_adjust_s dsp_cmd;
    struct rgb_gain_info mw_cmd;

    if(sgks_isp_CheckFwStatus() < 0)
    {
        return SGKS_FAIL;
    }

    memcpy(&mw_cmd, param, sizeof(mw_cmd));


    memset(&dsp_cmd, 0, sizeof(dsp_cmd));
    dsp_cmd.cmd_code 					= SGKS_DSP_CMD_RGB_GAIN_ADJUSTMENT;
    dsp_cmd.r_gain 						= mw_cmd.r_gain;
    dsp_cmd.g_even_gain 				= mw_cmd.g_even_gain;
    dsp_cmd.g_odd_gain 					= mw_cmd.g_odd_gain;
    dsp_cmd.b_gain 						= mw_cmd.b_gain;
    dsp_cmd.group_index 				= mw_cmd.group_index;
    dsp_cmd.group_index 				= 0;

    int ret = sgks_mpi_cmd_Send(&dsp_cmd, sizeof(dsp_cmd));
    if (ret != SGKS_SUCCESS)
    {
        return ret;
    }

    return SGKS_SUCCESS;

}

static int sgks_isp_cmd_AntiAliasingFilter(sgks_mpi_manager_s *mpi, struct anti_aliasing_info *param)
{
    sgks_dsp_cmd_anti_aliasing_filter_s dsp_cmd;
    struct anti_aliasing_info mw_cmd;

    if(sgks_isp_CheckFwStatus() < 0)
    {
        return SGKS_FAIL;
    }

    memcpy(&mw_cmd, param, sizeof(mw_cmd));

    memset(&dsp_cmd, 0, sizeof(dsp_cmd));
    dsp_cmd.cmd_code 				= SGKS_DSP_CMD_ANTI_ALIASING;
    dsp_cmd.enable 					= mw_cmd.enable;
    dsp_cmd.threshold 				= mw_cmd.threshold;
    dsp_cmd.shift 					= mw_cmd.shift;

    int ret = sgks_mpi_cmd_Send(&dsp_cmd, sizeof(dsp_cmd));
    if (ret != SGKS_SUCCESS)
    {
        return ret;
    }

    return SGKS_SUCCESS;

}

static int sgks_isp_cmd_DigitalGainSetLevel(sgks_mpi_manager_s *mpi, struct digital_gain_level *param)
{
    sgks_dsp_cmd_digital_gain_level_s dsp_cmd;
    struct digital_gain_level mw_cmd;

    if(sgks_isp_CheckFwStatus() < 0)
    {
        return SGKS_FAIL;
    }

    memcpy(&mw_cmd, param, sizeof(mw_cmd));


    memset(&dsp_cmd, 0, sizeof(dsp_cmd));
#if 0

    dsp_cmd.cmd_code = SGKS_DSP_CMD_DIGITAL_GAIN_SATURATION_LEVEL;
    dsp_cmd.level_red = mw_cmd.level_red;
    dsp_cmd.level_green_even = mw_cmd.level_green_even;
    dsp_cmd.level_green_odd = mw_cmd.level_green_odd;
    dsp_cmd.level_blue = mw_cmd.level_blue;
    dsp_cmd.group_index = mw_cmd.group_index;

#else

    dsp_cmd.cmd_code 				= SGKS_DSP_CMD_DIGITAL_GAIN_SATURATION_LEVEL;
    dsp_cmd.level_red 				= 16383;
    dsp_cmd.level_green_even 		= 16383;
    dsp_cmd.level_green_odd 		= 16383;
    dsp_cmd.level_blue 				= 16383;
    dsp_cmd.group_index 			= mw_cmd.group_index;

#endif

    int ret = sgks_mpi_cmd_Send(&dsp_cmd, sizeof(dsp_cmd));
    if (ret != SGKS_SUCCESS)
    {
        return ret;
    }

    return SGKS_SUCCESS;


}

static int sgks_isp_cmd_ZoomFactor(sgks_mpi_manager_s *mpi, zoom_factor_info_t *param)
{
    sgks_dsp_cmd_zoom_factor_s dsp_cmd;
    zoom_factor_info_t mw_cmd;

    if(sgks_isp_CheckFwStatus() < 0)
    {
        return SGKS_FAIL;
    }

    memcpy(&mw_cmd, param, sizeof(mw_cmd));

    memset(&dsp_cmd, 0, sizeof(dsp_cmd));
    dsp_cmd.cmd_code 				= SGKS_DSP_CMD_SET_ZOOM_FACTOR;
    dsp_cmd.zoom_x 					= mw_cmd.zoom_x;
    dsp_cmd.zoom_y 					= mw_cmd.zoom_y;
    dsp_cmd.x_center_offset 		= mw_cmd.x_center_offset;
    dsp_cmd.y_center_offset 		= mw_cmd.y_center_offset;

    int ret = sgks_mpi_cmd_Send(&dsp_cmd, sizeof(dsp_cmd));
    if (ret != SGKS_SUCCESS)
    {
        return ret;
    }

    return SGKS_SUCCESS;

}

static int sgks_isp_cmd_BadPixelCorrectSetup(sgks_mpi_manager_s *mpi, struct bad_pixel_correct_info *param)
{
    sgks_dsp_cmd_bad_pixel_correct_setup_s dsp_cmd;
    struct bad_pixel_correct_info mw_cmd;

    if(sgks_isp_CheckFwStatus() < 0)
    {
        return SGKS_FAIL;
    }

    memcpy(&mw_cmd, param, sizeof(mw_cmd));

    memset(&dsp_cmd, 0, sizeof(dsp_cmd));
    dsp_cmd.cmd_code 					= SGKS_DSP_CMD_BAD_PIXEL_CORRECT_SETUP;
    dsp_cmd.dynamic_bad_pixel_enable 	= mw_cmd.dynamic_bad_pixel_enable;
    dsp_cmd.correction_mode				= mw_cmd.correction_mode;
    dsp_cmd.hot_pixel_addr 				= mpi->isp_init_param.isp_hot_pixel_thd_table_phy;
    dsp_cmd.dark_pixel_addr 			= mpi->isp_init_param.isp_dark_pixel_thd_table_phy;
    dsp_cmd.shift0_4 					= mw_cmd.shift0_4;
    dsp_cmd.shift5 						= mw_cmd.shift5;
    dsp_cmd.static_bad_pixel_map_addr 	= 0;

    memcpy(mpi->isp_init_param.isp_hot_pixel_thd_table_vir, (void *)mw_cmd.hot_pixel_addr, DYN_BPC_THD_TABLE_SIZE);
    //sgks_isp_clean_cache(hot_pixel_thd_table, DYN_BPC_THD_TABLE_SIZE);

    memcpy(mpi->isp_init_param.isp_dark_pixel_thd_table_vir, (void *)mw_cmd.dark_pixel_addr, DYN_BPC_THD_TABLE_SIZE);
    //sgks_isp_clean_cache(dark_pixel_thd_table, DYN_BPC_THD_TABLE_SIZE);

    int ret = sgks_mpi_cmd_Send(&dsp_cmd, sizeof(dsp_cmd));
    if (ret != SGKS_SUCCESS)
    {
        return ret;
    }

    return SGKS_SUCCESS;

}

static int sgks_isp_cmd_DemoasicFilter(sgks_mpi_manager_s *mpi, struct demoasic_filter_info *param)
{
    sgks_dsp_cmd_demoasic_filter_s dsp_cmd;
    struct demoasic_filter_info mw_cmd;

    if(sgks_isp_CheckFwStatus() < 0)
    {
        return SGKS_FAIL;
    }

    memcpy(&mw_cmd, param, sizeof(mw_cmd));


    memset(&dsp_cmd, 0, sizeof(dsp_cmd));
    dsp_cmd.cmd_code 					= SGKS_DSP_CMD_DEMOASIC_FILTER;
    dsp_cmd.enable 						= mw_cmd.enable;
    dsp_cmd.grad_clip_thresh 			= mw_cmd.grad_clip_thresh;
    dsp_cmd.grad_noise_thresh 			= mw_cmd.grad_noise_thresh;
    //dsp_cmd.group_index 				= mw_cmd->group_index;
    dsp_cmd.activity_thresh 			= mw_cmd.act_thresh;
    dsp_cmd.activity_difference_thresh 	= mw_cmd.act_diff_thresh;

    int ret = sgks_mpi_cmd_Send(&dsp_cmd, sizeof(dsp_cmd));
    if (ret != SGKS_SUCCESS)
    {
        return ret;
    }

    return SGKS_SUCCESS;
}

static int sgks_isp_cmd_SensorInputSetup(sgks_mpi_manager_s *mpi, struct sensor_info *param)
{
    sgks_dsp_cmd_sensor_input_setup_s dsp_cmd;
    struct sensor_info mw_cmd;

    if(sgks_isp_CheckFwStatus() < 0)
    {
        return SGKS_FAIL;
    }

    memset(&dsp_cmd, 0, sizeof(dsp_cmd));

    memcpy(&mw_cmd, param, sizeof(mw_cmd));


    dsp_cmd.cmd_code 					= SGKS_DSP_CMD_SENSOR_INPUT_SETUP;
    dsp_cmd.sensor_id 					= mw_cmd.sensor_id;
    dsp_cmd.field_format 				= mw_cmd.field_format;
    dsp_cmd.sensor_resolution 			= mw_cmd.sensor_resolution;
    dsp_cmd.sensor_pattern 				= mw_cmd.sensor_pattern;
    dsp_cmd.first_line_field_0 			= mw_cmd.first_line_field_0;
    dsp_cmd.first_line_field_1 			= mw_cmd.first_line_field_1;
    dsp_cmd.first_line_field_2 			= mw_cmd.first_line_field_2;
    dsp_cmd.first_line_field_3 			= mw_cmd.first_line_field_3;
    dsp_cmd.first_line_field_4 			= mw_cmd.first_line_field_4;
    dsp_cmd.first_line_field_5 			= mw_cmd.first_line_field_5;
    dsp_cmd.first_line_field_6 			= mw_cmd.first_line_field_6;
    dsp_cmd.first_line_field_7 			= mw_cmd.first_line_field_7;
    dsp_cmd.sensor_readout_mode 		= mw_cmd.sensor_readout_mode;

    int ret = sgks_mpi_cmd_Send(&dsp_cmd, sizeof(dsp_cmd));
    if (ret != SGKS_SUCCESS)
    {
        return ret;
    }

    return SGKS_SUCCESS;
}


/**
 * Fixed pattern noise correction
 */
static int sgks_isp_cmd_FixedPatternNoiseCorrect(sgks_mpi_manager_s *mpi, struct fixed_pattern_correct *param)
{
    sgks_dsp_cmd_fixed_pattern_noise_correct_s dsp_cmd;
    struct fixed_pattern_correct mw_cmd;
    memset(&dsp_cmd, 0, sizeof(dsp_cmd));

    memcpy(&mw_cmd, param, sizeof(mw_cmd));


    dsp_cmd.cmd_code 							= SGKS_DSP_CMD_FIXED_PATTERN_NOISE_CORRECTION;
    dsp_cmd.fpn_pixel_mode 						= 3;
    dsp_cmd.num_of_rows 						= mw_cmd.num_of_rows;
    dsp_cmd.num_of_cols 						= mw_cmd.num_of_cols;
    dsp_cmd.fpn_pitch 							= mw_cmd.fpn_pitch;
    dsp_cmd.fpn_pixels_addr 					= mpi->isp_init_param.isp_pixel_map_addr_phy;
    dsp_cmd.intercepts_and_slopes_addr 			= mpi->isp_init_param.isp_fpn_reg_addr_phy;

    if(mw_cmd.fpn_pixels_buf_size > 384 * 1944)
    {
        return -1;
    }

    dsp_cmd.fpn_pixels_buf_size 				= mw_cmd.fpn_pixels_buf_size;
    dsp_cmd.intercept_shift 					= 3;
    dsp_cmd.row_gain_enable 					= 0;
    dsp_cmd.row_gain_addr 						= 0;
    dsp_cmd.column_gain_enable 					= 0;
    dsp_cmd.column_gain_addr 					= 0;

    memcpy(mpi->isp_init_param.isp_pixel_map_addr_vir, (void *)mw_cmd.fpn_pixels_addr, dsp_cmd.fpn_pixels_buf_size);
    //sgks_isp_clean_cache(pixel_map_addr, dsp_cmd.fpn_pixels_buf_size);

    int ret = sgks_mpi_cmd_Send(&dsp_cmd, sizeof(dsp_cmd));
    if (ret != SGKS_SUCCESS)
    {
        return ret;
    }

    return SGKS_SUCCESS;

}


int sgks_isp_StatisticsReady(void)
{

#if 0
//    if (g_wait_count > 0)
    {
        complete(&g_statis_comp);
//        g_wait_count--;
    }
#endif

    return 0;
}

int sgks_mpi_isp_cmd(u32 cmd, u8 *arg)
{
    int ret = 0;

    sgks_mpi_manager_s *mpi = mpi_get_managerHanle();
    //static int i = 0;
    //Printf("sgks_mpi_isp_cmd %d  0x%x\n", i++, cmd);


    switch(cmd)
    {
    case  SGKS_MDI_IMG_GET_STATISTICS:
        ret = sgks_isp_cmd_GetStatistics(mpi, (struct isp_statistics *)arg);
        break;


    case    SGKS_MDI_IMG_CONFIG_STATISTICS:
        ret = sgks_isp_cmd_3AStatisticsSetup(mpi, (SGKS_AAA_STATISTICS_CONFIG_S *)arg);
        break;


    case    SGKS_MDI_IMG_AAA_SETUP_EX:
        ret = sgks_isp_cmd_3AStatisticsEx(mpi, (struct aaa_statistics_ex *)arg);
        break;

    case    SGKS_MDI_IMG_NOISE_FILTER_SETUP:
        ret = sgks_isp_cmd_NoiseFilterSetup(mpi, (u32)arg);
        break;


    case    SGKS_MDI_IMG_BLACK_LEVEL_GLOBAL_OFFSET:
        ret = sgks_isp_cmd_BlackLevelGlobalOffset(mpi, (struct black_level_global_offset *)arg);
        break;


    case    SGKS_MDI_IMG_BAD_PIXEL_CORRECTION:
        ret = sgks_isp_cmd_BadPixelCorrectSetup(mpi, (struct bad_pixel_correct_info *)arg);
        break;


    case    SGKS_MDI_IMG_CFA_LEAKAGE_FILTER_SETUP:
        ret = sgks_isp_cmd_CfaDomainLeakageFilterSetup(mpi, (struct cfa_leakage_filter_info *)arg);
        break;

    case    SGKS_MDI_IMG_CFA_NOISE_FILTER_SETUP:
        ret = sgks_isp_cmd_CfaNoiseFilter(mpi, (cfa_noise_filter_info_t *)arg);
        break;


    case    SGKS_MDI_IMG_VIGNETTE_COMPENSATION:
        ret = sgks_isp_cmd_VignetteCompensation(mpi, (struct vignette_compensation_info *)arg);
        break;


    case   SGKS_MDI_IMG_LOCAL_EXPOSURE:
        ret = sgks_isp_cmd_LocalExposure(mpi, (struct local_exposure_info *)arg);
        break;


    case    SGKS_MDI_IMG_COLOR_CORRECTION:
        ret = sgks_isp_cmd_ColorCorrection(mpi, (struct color_correction_info *)arg);
        break;


    case    SGKS_MDI_IMG_RGB_TO_YUV_SETUP:
        ret = sgks_isp_cmd_RgbToYuvSetup(mpi, (struct rgb_to_yuv_info *)arg);
        break;


    case    SGKS_MDI_IMG_CHROMA_SCALE:
        ret = sgks_isp_cmd_ChromaScale(mpi, (struct chroma_scale_info *)arg);
        break;


    case    SGKS_MDI_IMG_CHROMA_MEDIAN_FILTER_SETUP:
        ret = sgks_isp_cmd_ChromaMedianFilter(mpi, (struct chroma_median_filter *)arg);
        break;


    case    SGKS_MDI_IMG_LUMA_SHARPENING:
        ret = sgks_isp_cmd_LumaSharpening(mpi, (struct luma_sharpening_info *)arg);
        break;


    case    SGKS_MDI_IMG_LUMA_SHARPENING_FIR_CONFIG:
        ret = sgks_isp_cmd_LumaSharpFir(mpi, (struct luma_sharp_fir_info *)arg);
        break;


    case    SGKS_MDI_IMG_LUMA_SHARPENING_EDGE_CONTROL:
        ret = sgks_isp_cmd_LumaSharpenEdgeCtrl(mpi, (struct luma_sharp_edge_info *)arg);
        break;


    case    SGKS_MDI_IMG_LUMA_SHARPENING_BLEND_CONFIG:
        ret = sgks_isp_cmd_LumaSharpenBlendCtrl(mpi, (struct luma_sharp_blend_info *)arg);
        break;


    case    SGKS_MDI_IMG_LUMA_SHARPENING_LEVEL_CONTROL:
        ret = sgks_isp_cmd_LumaSharpenLevelCtrl(mpi, (struct luma_sharp_level_info *)arg);
        break;


    case    SGKS_MDI_IMG_LUMA_SHARPENING_MISC:
        ret = sgks_isp_cmd_LumaSharpenMiscCtrl(mpi, (struct luma_sharp_misc_info *)arg);
        break;


    case    SGKS_MDI_IMG_MCTF_GMV_SETUP:
        ret = sgks_isp_cmd_MctfGmvSetup(mpi, (struct mctf_gmv_info *)arg);
        break;


    case    SGKS_MDI_IMG_MCTF_MV_STABILIZER_SETUP:
        ret = sgks_isp_cmd_Mctf_MV_StabilizerSetup(mpi, (struct mctf_mv_stab_info *)arg);
        break;


    case    SGKS_MDI_IMG_RGB_GAIN_ADJUST:
        ret = sgks_isp_cmd_RgbGainAdjustment(mpi, (struct rgb_gain_info *)arg);
        break;


    case    SGKS_MDI_IMG_ANTI_ALIASING_CONFIG:
        ret = sgks_isp_cmd_AntiAliasingFilter(mpi, (struct anti_aliasing_info *)arg);
        break;


    case    SGKS_MDI_IMG_DIGITAL_SATURATION_LEVEL:
        ret = sgks_isp_cmd_DigitalGainSetLevel(mpi, (struct digital_gain_level *)arg);
        break;


    case    SGKS_MDI_IMG_SET_ZOOM_FACTOR:
        ret = sgks_isp_cmd_ZoomFactor(mpi, (zoom_factor_info_t *)arg);
        break;


    case    SGKS_MDI_IMG_DEMOSAIC_CONIFG:
        ret = sgks_isp_cmd_DemoasicFilter(mpi, (struct demoasic_filter_info *)arg);
        break;


    case    SGKS_MDI_IMG_SENSOR_CONFIG:
        ret = sgks_isp_cmd_SensorInputSetup(mpi, (struct sensor_info *)arg);
        break;


    case    SGKS_MDI_IMG_STATIC_BAD_PIXEL_CORRECTION:
        ret = sgks_isp_cmd_FixedPatternNoiseCorrect(mpi, (struct fixed_pattern_correct *)arg);
        break;


    case     SGKS_MDI_IMG_CANCEL_GETTING_STATISTICS:
        ret = sgks_isp_StatisticsReady();
        break;


    default:
        Printf("!!!====sgks isp cmd err!\n");
        ret = SGKS_FAIL;
        break;


    }

    return ret;


}


int sgks_mpi_isp_Disable(sgks_mpi_manager_s *mpi)
{
    if (mpi->isp_init_param.enable != 1)
   	{
		return 0;
   	}
	
    int ret =  sgks_image_isp_stop(mpi->ispToImageHandle);
    if (ret != SGKS_SUCCESS)
    {
        Printf("sgks_mpi_isp_Disable Fail, nRet=%d\n", ret);
        return SGKS_ERR_ISP_INIT_FAIL;
    }

	Printf("sgks_mpi_isp_Disable ok\n");
	
    return SGKS_SUCCESS;

}


inline int sgks_mpi_isp_Enable(sgks_mpi_manager_s *mpi)
{

   	if (mpi->isp_init_param.enable != 1)
   	{
		return 0;
   	}

    int ret =  sgks_image_isp_start(mpi->ispToImageHandle);
    if (ret != SGKS_SUCCESS)
    {
        Printf("sgks_mpi_isp_Enable Fail, nRet=%d\n", ret);
        return SGKS_ERR_ISP_INIT_FAIL;
    }

	Printf("sgks_mpi_isp_Enable ok\n");
	
    return SGKS_SUCCESS;

}


int sgks_mpi_isp_stop(sgks_mpi_manager_s *mpi)
{

	if (mpi->isp_init_param.enable != 1)
   	{
		return 0;
   	}

    int ret = sgks_image_close(mpi->ispToImageHandle);
    if (ret != SGKS_SUCCESS)
    {
        Printf("sgks_image_close Fail\n");
        return SGKS_ERR_ISP_DEINIT_FAIL;
    }

    ret = sgks_image_exit();
    if (ret != SGKS_SUCCESS)
        return SGKS_ERR_ISP_DEINIT_FAIL;

    Printf("sgks_mpi_isp_stop ok\n");

    return SGKS_SUCCESS;

}


int sgks_mpi_isp_start(sgks_mpi_manager_s *mpi)
{

    int ret = 0;

    Printf("sgks_mpi_isp_start enter\n");

    sgks_vi_src_capability_s vi_cap_ability[MAX_VI_NUM];
    sgks_vi_src_capability_s *current_vi_info  = NULL;
    ret = sgks_mpi_vi_GetSensorCapability(vi_cap_ability);   
    if (ret)
    {
        return ret;
    }
    current_vi_info = &vi_cap_ability[0];

    // if current input_format is not raw
    if (current_vi_info->input_format != 0)
    {
        return 0;
    }

    if (mpi->isp_init_param.enable == 1)
    {

        ret = sgks_image_init();
        if (ret != SGKS_SUCCESS)
        {
            Printf("sgks_image_init, nRet=%d\n", ret);
            return SGKS_ERR_ISP_INIT_FAIL;
        }

        SGKS_ISP_OpenParamsT openParams;
        memset(&openParams, 0, sizeof(openParams));
        mpi->ispToImageHandle = sgks_image_open(&openParams, &ret);
        if (mpi->ispToImageHandle == NULL || ret != SGKS_SUCCESS)
        {
            Printf("sgks_image_open failed nRet=%d\n", ret);
            return SGKS_ERR_ISP_INIT_FAIL;
        }

        ret = sgks_image_load_param(mpi->ispToImageHandle, NULL);
        if (ret != SGKS_SUCCESS)
        {
            Printf("sgks_image_load_param Fail, nRet=%d\n", ret);
            return SGKS_ERR_ISP_INIT_FAIL;
        }

        ret =  sgks_mpi_isp_Enable(mpi);
        if (ret != SGKS_SUCCESS)
        {
            Printf("sgks_image_start Fail, nRet=%d\n", ret);
            return SGKS_ERR_ISP_INIT_FAIL;
        }

#if 0        
        ret =  sgks_mpi_isp_ResetAAAStats();
        if (ret != SGKS_SUCCESS)
        {
            Printf("sgks_mpi_isp_ResetAAAStats Fail, nRet=%d\n", ret);
            return SGKS_ERR_ISP_INIT_FAIL;
        }  
#endif
        ret =  sgks_mpi_isp_SetBrightness(128);
        if (ret != SGKS_SUCCESS)
        {
            Printf("sgks_image_start Fail, nRet=%d\n", ret);
            return SGKS_ERR_ISP_INIT_FAIL;
        }

        Printf("sgks_mpi_isp_start ok\n");

    }


    return ret;

}


int sgks_mpi_isp_Init(sgks_isp_initParam_s *isp_config)
{

    int ret = -1;

    sgks_mpi_manager_s *mpi = mpi_get_managerHanle();

    if (sgks_mpi_sys_GetModuleInitStatus(mpi ,MPI_MODULE_ISP_ID) == MPI_MODULE_STATUS_INIT)
    {
        Printf("sgks_mpi_isp already init\n");

        mpi->curr_module_id		= MPI_MODULE_ISP_ID;

        ret = sgks_mpi_sys_GetDriverHandle(mpi);
        if (ret != SGKS_SUCCESS)
            return ret;

        ret = sgks_mpi_vi_OpenDeviceNode(mpi);
        if (ret != SGKS_SUCCESS)
        {
            return ret;
        }

        ret = sgks_mpi_isp_MmapPhyAddr(&mpi->isp_init_param);
        if (ret != SGKS_SUCCESS)
        {
            return ret;
        }

        return SGKS_SUCCESS;
		
    }

    mpi->curr_module_id		= MPI_MODULE_ISP_ID;

    ret = sgks_mpi_sys_GetDriverHandle(mpi);
    if (ret != SGKS_SUCCESS)
        return ret;

    ret = sgks_mpi_vi_OpenDeviceNode(mpi);
    if (ret != SGKS_SUCCESS)
    {
        return ret;
    }

    ret = sgks_mpi_isp_CheckMemAddrPageAlign(isp_config);
    if (ret != SGKS_SUCCESS)
    {
        return ret;
    }

    memcpy(&mpi->isp_init_param, isp_config, sizeof(sgks_isp_initParam_s));

    ret = sgks_mpi_isp_MmapPhyAddr(&mpi->isp_init_param);
    if (ret != SGKS_SUCCESS)
    {
        return ret;
    }

    u8 *msg_buf = NULL;
    SGKS_MDI_DRV_BUFF(msg_buf);
    sgks_isp_buffer_config_s *isp_buf = msg_buf;
    isp_buf->isp_aaa_fifo1_base		   = mpi->isp_init_param.isp_aaa_fifo1_base_phy;
    isp_buf->isp_aaa_fifo1_base_size   = mpi->isp_init_param.isp_aaa_fifo1_base_size;

    isp_buf->isp_aaa_fifo2_base		   = mpi->isp_init_param.isp_aaa_fifo2_base_phy;
    isp_buf->isp_aaa_fifo2_base_size   = mpi->isp_init_param.isp_aaa_fifo2_base_size;

    SGKS_MDI_DRV_IOCTL(SGKS_MDI_OPERATION_SETISPBUFFER, 0, sizeof(sgks_isp_buffer_config_s), SGKS_ERR_SYS_DRIVERIOCTLERR);

    sgks_mpi_sys_UpdateModuleInitStatus(mpi, MPI_MODULE_ISP_ID, MPI_MODULE_STATUS_INIT);

    Printf("sgks_mpi_isp_Init ok!\n");

    return SGKS_SUCCESS;

}


int sgks_mpi_isp_DeInit()
{
	int ret = -1;
	
	sgks_mpi_manager_s *mpi = mpi_get_managerHanle();


	ret = sgks_mpi_isp_Disable(mpi);
        if (ret != SGKS_SUCCESS)
            return ret;

	ret = sgks_mpi_isp_stop(mpi);
        if (ret != SGKS_SUCCESS)
            return ret;

	ret = sgks_mpi_isp_UnMmapPhyAddr(&mpi->isp_init_param);
	if (ret != SGKS_SUCCESS)
		 return ret;
	
    return SGKS_SUCCESS;

}


int sgks_mpi_isp_SetMpiToIspHandle(sgks_mpi_manager_s *mpi)
{


    u8 *msg_buf									= NULL;
    SGKS_MDI_DRV_BUFF(msg_buf);
    sgks_mdi_main_mpi_s *mpi_main 				= msg_buf;

    mpi->isp_CB.isp_CmdCallBack 				= sgks_mpi_isp_cmd;
    mpi->isp_CB.isp_GetFrameTime			    = sgks_mpi_vi_GetFramerateTime;
    mpi->isp_CB.isp_GetMpiState					= sgks_mpi_sys_GetStats;
    mpi->isp_CB.isp_MdiCallBack			    	= sgks_mpi_mdi_cmd;
    mpi->isp_CB.isp_ConfigSensor				= sgks_mpi_vi_isp_senseor;

    mpi_main->isp_CmdCallBack 		   			= mpi->isp_CB.isp_CmdCallBack;
    mpi_main->isp_GetFrameTime					= mpi->isp_CB.isp_GetFrameTime;
    mpi_main->isp_GetMpiState					= mpi->isp_CB.isp_GetMpiState;
    mpi_main->isp_MdiCallBack					= mpi->isp_CB.isp_MdiCallBack;
    mpi_main->isp_ConfigSensor					= mpi->isp_CB.isp_ConfigSensor;

    Printf("0x%x,  0x%x,   0x%x,  0x%x,  0x%x\n"
           ,mpi_main->isp_CmdCallBack
           ,mpi_main->isp_GetFrameTime
           ,mpi_main->isp_GetMpiState
           ,mpi_main->isp_MdiCallBack
           ,mpi_main->isp_ConfigSensor);

    SGKS_MDI_DRV_IOCTL(SGKS_MDI_OPERATION_SETMAINMPI, 0, sizeof(sgks_mdi_main_mpi_s), SGKS_ERR_SYS_DRIVERIOCTLERR);


    return SGKS_SUCCESS;


}

int sgks_mpi_isp_SetBrightness(int nBrightness)
{
    int nRet = SGKS_SUCCESS;
    sgks_mpi_manager_s *mpi = NULL;
    void *ispHandle = NULL;

    mpi = mpi_get_managerHanle();
    if (mpi==NULL)
    {    
        ERR_PRINT("get mpi manager error!\n");
        return SGKS_FAIL;
    }

    ispHandle = mpi->ispToImageHandle;
    if (ispHandle==NULL)
    {    
        return SGKS_FAIL;
    }    

    if (nBrightness<1)
        nBrightness = 1;

    if (nBrightness>255)
        nBrightness = 255;

    nRet = gadi_isp_set_brightness(ispHandle, nBrightness);
    if(nRet != SGKS_SUCCESS)
    {
        return SGKS_ERR_ISP_FUNCTION_ERR;
    }

    return nRet;
}

int sgks_mpi_isp_GetBrightness(int *pBrightness)
{
    int nRet = SGKS_SUCCESS;
    sgks_mpi_manager_s *mpi = NULL;
    void *ispHandle = NULL;

    mpi = mpi_get_managerHanle();
    if (mpi==NULL)
    {    
        ERR_PRINT("get mpi manager error!\n");
        return SGKS_FAIL;
    }

    ispHandle = mpi->ispToImageHandle;
    if (ispHandle==NULL)
    {    
        return SGKS_FAIL;
    }    

    if (pBrightness==NULL)
    {
        return SGKS_ERR_ISP_PARAM_ERR;    
    }

    nRet = gadi_isp_get_brightness(ispHandle, pBrightness);
    if(nRet != SGKS_SUCCESS)
    {
        return SGKS_ERR_ISP_FUNCTION_ERR;
    }

    return nRet;
}


int sgks_mpi_isp_SetContrast(int enableAuto, int nContrast)
{
    int nRet = SGKS_SUCCESS;
    sgks_mpi_manager_s *mpi = NULL;
    sgks_mpi_isp_ContrastAttr_s contrastattr;
    void *ispHandle = NULL;

    mpi = mpi_get_managerHanle();
    if (mpi==NULL)
    {    
        ERR_PRINT("get mpi manager error!\n");
        return SGKS_FAIL;
    }

    ispHandle = mpi->ispToImageHandle;
    if (ispHandle==NULL)
    {    
        return SGKS_FAIL;
    }

    if (nContrast<0)
        nContrast = 0;

    if (nContrast>0xff)
        nContrast = 0xff;

    contrastattr.enableAuto     = enableAuto;
    contrastattr.autoStrength   = nContrast;
    contrastattr.manualStrength = nContrast;

    nRet = gadi_isp_set_contrast_attr(ispHandle, &contrastattr);
    if(nRet != SGKS_SUCCESS)
    {
        return SGKS_ERR_ISP_FUNCTION_ERR;
    }

    return nRet;
}

int sgks_mpi_isp_SetSaturation(int nSaturation)
{
    int nRet = SGKS_SUCCESS;
    sgks_mpi_manager_s *mpi = NULL;
    void *ispHandle = NULL;

    mpi = mpi_get_managerHanle();
    if (mpi==NULL)
    {    
        ERR_PRINT("get mpi manager error!\n");
        return SGKS_FAIL;
    }

    ispHandle = mpi->ispToImageHandle;
    if (ispHandle==NULL)
    {    
        return SGKS_FAIL;
    }

    if (nSaturation<0)
        nSaturation = 0;

    if (nSaturation>255)
        nSaturation = 255;

    nRet = gadi_isp_set_saturation(ispHandle, nSaturation);
    if(nRet != SGKS_SUCCESS)
    {
        return SGKS_ERR_ISP_FUNCTION_ERR;
    }

    return nRet;
}

int sgks_mpi_isp_GetSaturation(int *pSaturation)
{
    int nRet = SGKS_SUCCESS;
    sgks_mpi_manager_s *mpi = NULL;
    void *ispHandle = NULL;

    mpi = mpi_get_managerHanle();
    if (mpi==NULL)
    {    
        ERR_PRINT("get mpi manager error!\n");
        return SGKS_FAIL;
    }

    ispHandle = mpi->ispToImageHandle;
    if (ispHandle==NULL)
    {    
        return SGKS_FAIL;
    }

    if (pSaturation==NULL)
    {
        return SGKS_ERR_ISP_PARAM_ERR;    
    }

    nRet = gadi_isp_get_saturation(ispHandle, pSaturation);
    if(nRet != SGKS_SUCCESS)
    {
        return SGKS_ERR_ISP_FUNCTION_ERR;
    }

    return nRet;
}

/*
int sgks_mpi_isp_SetHue(int nHue)
{
    int nRet = SGKS_SUCCESS;
    sgks_mpi_manager_s *mpi = NULL;
    void *ispHandle = NULL;

    mpi = mpi_get_managerHanle();
    if (mpi==NULL)
    {    
        ERR_PRINT("get mpi manager error!\n");
        return SGKS_FAIL;
    }

    ispHandle = mpi->ispToImageHandle;
    if (ispHandle==NULL)
    {    
        return SGKS_FAIL;
    }

    nRet = gadi_isp_set_hue(ispHandle, nHue);
    if(nRet != SGKS_SUCCESS)
    {
        return SGKS_ERR_ISP_FUNCTION_ERR;
    }

    return nRet;
}

int sgks_mpi_isp_GetHue(int *pHue)
{
    int nRet = SGKS_SUCCESS;
    sgks_mpi_manager_s *mpi = NULL;
    void *ispHandle = NULL;

    mpi = mpi_get_managerHanle();
    if (mpi==NULL)
    {    
        ERR_PRINT("get mpi manager error!\n");
        return SGKS_FAIL;
    }

    ispHandle = mpi->ispToImageHandle;
    if (ispHandle==NULL)
    {    
        return SGKS_FAIL;
    }

    if (pHue==NULL)
    {    
        return SGKS_ERR_ISP_PARAM_ERR;
    }

    nRet = gadi_isp_get_hue(ispHandle, pHue);
    if(nRet != SGKS_SUCCESS)
    {
        return SGKS_ERR_ISP_FUNCTION_ERR;
    }

    return nRet;
}
*/

int sgks_mpi_isp_SetExposureMeterMode(sgks_mpi_isp_ae_metering_mode_e mode)
{
    int nRet = SGKS_SUCCESS;
    sgks_mpi_manager_s *mpi = NULL;
    void *ispHandle = NULL;

    mpi = mpi_get_managerHanle();
    if (mpi==NULL)
    {    
        ERR_PRINT("get mpi manager error!\n");
        return SGKS_FAIL;
    }

    ispHandle = mpi->ispToImageHandle;
    if (ispHandle==NULL)
    {    
        return SGKS_FAIL;
    }

    nRet = gadi_isp_set_meter_mode(mode);
    if(nRet != SGKS_SUCCESS)
    {
        return SGKS_ERR_ISP_FUNCTION_ERR;
    }

    return nRet;
}

int sgks_mpi_isp_SetExposureRoi(int roi[96])
{
    int nRet = SGKS_SUCCESS;
    sgks_mpi_manager_s *mpi = NULL;
    void *ispHandle = NULL;

    mpi = mpi_get_managerHanle();
    if (mpi==NULL)
    {    
        ERR_PRINT("get mpi manager error!\n");
        return SGKS_FAIL;
    }

    ispHandle = mpi->ispToImageHandle;
    if (ispHandle==NULL)
    {    
        return SGKS_FAIL;
    }

    if (roi==NULL)
    {
        return SGKS_ERR_ISP_PARAM_ERR;
    }
    
    nRet = gadi_isp_set_roi(roi);
    if(nRet != SGKS_SUCCESS)
    {
        return SGKS_ERR_ISP_FUNCTION_ERR;
    }

    return nRet;
}


int sgks_mpi_isp_SetExposureType(sgks_mpi_isp_ExposureType_e type)
{
    int nRet = SGKS_SUCCESS;
    sgks_mpi_manager_s *mpi = NULL;
    void *ispHandle = NULL;

    mpi = mpi_get_managerHanle();
    if (mpi==NULL)
    {    
        ERR_PRINT("get mpi manager error!\n");
        return SGKS_FAIL;
    }

    ispHandle = mpi->ispToImageHandle;
    if (ispHandle==NULL)
    {    
        return SGKS_FAIL;
    }

    if (type!=SGKS_ISP_EXPOSURE_TYPE_AUTO && type!=SGKS_ISP_EXPOSURE_TYPE_MANUAL)
    {
        return SGKS_ERR_ISP_PARAM_ERR;
    }
    
    nRet = gadi_isp_set_exposure_type(ispHandle, type);
    if(nRet != SGKS_SUCCESS)
    {
        return SGKS_ERR_ISP_FUNCTION_ERR;
    }

    return nRet;
}

int sgks_mpi_isp_GetExposureCurrentGain(u32 *p_gain)
{
    int nRet = SGKS_SUCCESS;
    sgks_mpi_manager_s *mpi = NULL;
    void *ispHandle = NULL;

    mpi = mpi_get_managerHanle();
    if (mpi==NULL)
    {    
        ERR_PRINT("get mpi manager error!\n");
        return SGKS_FAIL;
    }

    ispHandle = mpi->ispToImageHandle;
    if (ispHandle==NULL)
    {    
        return SGKS_FAIL;
    }

    if(p_gain == NULL)
    {
        return SGKS_ERR_ISP_PARAM_ERR;
    }

    nRet =  gadi_isp_get_current_gain(ispHandle, p_gain);
    if(nRet != SGKS_SUCCESS)
    {
        return SGKS_ERR_ISP_FUNCTION_ERR;
    }

    return SGKS_SUCCESS;
}

int sgks_mpi_isp_GetExposureCurrentShutter(double *p_shutter)
{
    int nRet = SGKS_SUCCESS;
    sgks_mpi_manager_s *mpi = NULL;
    void *ispHandle = NULL;

    mpi = mpi_get_managerHanle();
    if (mpi==NULL)
    {    
        ERR_PRINT("get mpi manager error!\n");
        return SGKS_FAIL;
    }

    ispHandle = mpi->ispToImageHandle;
    if (ispHandle==NULL)
    {    
        return SGKS_FAIL;
    }

    if(p_shutter == NULL)
    {
        return SGKS_ERR_ISP_PARAM_ERR;
    }

    nRet =  gadi_isp_get_current_shutter(ispHandle, p_shutter);
    if(nRet != SGKS_SUCCESS)
    {
        return SGKS_ERR_ISP_FUNCTION_ERR;
    }

    return SGKS_SUCCESS;
}

int sgks_mpi_isp_GetExposureCurrentTarget(int *p_target)
{
    *p_target = gadi_isp_get_ae_target();
    return 0;
}

int sgks_mpi_isp_GetExposureCurrentLuma(int *p_luma)
{
    *p_luma = (int)gadi_isp_get_y_result();
    return 0;
}

int sgks_mpi_isp_SetAeEvBias(int ev_bias)
{
    int nRet = SGKS_SUCCESS;
    sgks_mpi_manager_s *mpi = NULL;
    void *ispHandle = NULL;

    mpi = mpi_get_managerHanle();
    if (mpi==NULL)
    {    
        ERR_PRINT("get mpi manager error!\n");
        return SGKS_FAIL;
    }

    ispHandle = mpi->ispToImageHandle;
    if (ispHandle==NULL)
    {    
        return SGKS_FAIL;
    }

    if((ev_bias<-512) || (ev_bias>512))
    {
        return SGKS_ERR_ISP_PARAM_ERR;
    }

    nRet =  gadi_isp_set_ae_ev_bias(ev_bias);
    if(nRet != SGKS_SUCCESS)
    {
        return SGKS_ERR_ISP_FUNCTION_ERR;
    }

    return SGKS_SUCCESS;
}

int sgks_mpi_isp_SetAeSpeed(u8 ae_speed)
{
    return gadi_isp_set_ae_speed(ae_speed);
}

int sgks_mpi_isp_SetAeAttr(sgks_mpi_isp_AeAttr_s AeAttr)
{
    int nRet = SGKS_SUCCESS;
    sgks_mpi_manager_s *mpi = NULL;
    void *ispHandle = NULL;

    mpi = mpi_get_managerHanle();
    if (mpi==NULL)
    {    
        ERR_PRINT("get mpi manager error!\n");
        return SGKS_FAIL;
    }

    ispHandle = mpi->ispToImageHandle;
    if (ispHandle==NULL)
    {    
        return SGKS_FAIL;
    }

    nRet = gadi_isp_set_ae_attr(ispHandle, &AeAttr);
    if(nRet != SGKS_SUCCESS)
    {
        return SGKS_ERR_ISP_FUNCTION_ERR;
    }

    return nRet;
}

int sgks_mpi_isp_GetAeAttr(sgks_mpi_isp_AeAttr_s *pAeAttr)
{
    int nRet = SGKS_SUCCESS;
    sgks_mpi_manager_s *mpi = NULL;
    void *ispHandle = NULL;

    mpi = mpi_get_managerHanle();
    if (mpi==NULL)
    {    
        ERR_PRINT("get mpi manager error!\n");
        return SGKS_FAIL;
    }

    ispHandle = mpi->ispToImageHandle;
    if (ispHandle==NULL)
    {    
        return SGKS_FAIL;
    }

    if(pAeAttr == NULL)
    {
        return SGKS_ERR_ISP_PARAM_ERR;
    }

    nRet = gadi_isp_get_ae_attr(ispHandle, pAeAttr);
    if(nRet != SGKS_SUCCESS)
    {
        return SGKS_ERR_ISP_FUNCTION_ERR;
    }

    return nRet;
}

int sgks_mpi_isp_SetMeAttr(sgks_mpi_isp_MeAttr_s MeAttr)
{
    int nRet = SGKS_SUCCESS;
    sgks_mpi_manager_s *mpi = NULL;
    void *ispHandle = NULL;

    mpi = mpi_get_managerHanle();
    if (mpi==NULL)
    {    
        ERR_PRINT("get mpi manager error!\n");
        return SGKS_FAIL;
    }

    ispHandle = mpi->ispToImageHandle;
    if (ispHandle==NULL)
    {    
        return SGKS_FAIL;
    }
    
    nRet = gadi_isp_set_me_attr(ispHandle, &MeAttr);
    if(nRet != SGKS_SUCCESS)
    {
        return SGKS_ERR_ISP_FUNCTION_ERR;
    }

    return nRet;
}

int sgks_mpi_isp_GetMeAttr(sgks_mpi_isp_MeAttr_s *pMeAttr)
{
    int nRet = SGKS_SUCCESS;
    sgks_mpi_manager_s *mpi = NULL;
    void *ispHandle = NULL;

    mpi = mpi_get_managerHanle();
    if (mpi==NULL)
    {    
        ERR_PRINT("get mpi manager error!\n");
        return SGKS_FAIL;
    }

    ispHandle = mpi->ispToImageHandle;
    if (ispHandle==NULL)
    {    
        return SGKS_FAIL;
    }

    nRet = gadi_isp_get_me_attr(ispHandle, pMeAttr);
    if(nRet != SGKS_SUCCESS)
    {
        return SGKS_ERR_ISP_FUNCTION_ERR;
    }

    return nRet;
}

#if 0
int sgks_mpi_isp_GetSystemGain(int *pGain)
{
    int nRet = SGKS_SUCCESS;
    sgks_mpi_manager_s *mpi = NULL;
    void *ispHandle = NULL;

    mpi = mpi_get_managerHanle();
    if (mpi==NULL)
    {    
        ERR_PRINT("get mpi manager error!\n");
        return SGKS_FAIL;
    }

    ispHandle = mpi->ispToImageHandle;
    if (ispHandle==NULL)
    {    
        return SGKS_FAIL;
    }

    if(pGain == NULL)
    {
        return SGKS_ERR_ISP_PARAM_ERR;
    }

    nRet =  gadi_isp_get_systemgain(ispHandle, pGain);
    if(nRet != SGKS_SUCCESS)
    {
        return SGKS_ERR_ISP_FUNCTION_ERR;
    }

    return SGKS_SUCCESS;
}
#endif

int sgks_mpi_isp_SetWhiteBalanceType(sgks_mpi_isp_WhiteBalanceType_e type)
{
    int nRet = SGKS_SUCCESS;
    sgks_mpi_manager_s *mpi = NULL;
    void *ispHandle = NULL;

    mpi = mpi_get_managerHanle();
    if (mpi==NULL)
    {    
        ERR_PRINT("get mpi manager error!\n");
        return SGKS_FAIL;
    }

    ispHandle = mpi->ispToImageHandle;
    if (ispHandle==NULL)
    {    
        return SGKS_FAIL;
    }

    if (type!=SGKS_ISP_WB_TYPE_AUTO && type!=SGKS_ISP_WB_TYPE_MANUAL)
    {
        return SGKS_ERR_ISP_PARAM_ERR;
    }

    nRet = gadi_isp_set_wb_type(ispHandle, type);
    if(nRet != SGKS_SUCCESS)
    {
        return SGKS_ERR_ISP_FUNCTION_ERR;
    }

    return nRet;
}

int sgks_mpi_isp_SetAwbSpeed(unsigned char awb_speed)
{
    return gadi_isp_set_awb_speed(awb_speed);
}


int sgks_mpi_isp_SetMwbAttr(sgks_mpi_isp_MwbAttr_s MwbAttr)
{
    int nRet = SGKS_SUCCESS;
    sgks_mpi_manager_s *mpi = NULL;
    void *ispHandle = NULL;

    mpi = mpi_get_managerHanle();
    if (mpi==NULL)
    {    
        ERR_PRINT("get mpi manager error!\n");
        return SGKS_FAIL;
    }

    ispHandle = mpi->ispToImageHandle;
    if (ispHandle==NULL)
    {    
        return SGKS_FAIL;
    }
    
    nRet = gadi_isp_set_mwb_attr(ispHandle, &MwbAttr);
    if(nRet != SGKS_SUCCESS)
    {
        return SGKS_ERR_ISP_FUNCTION_ERR;
    }

    return nRet;
}

int sgks_mpi_isp_GetMwbAttr(sgks_mpi_isp_MwbAttr_s *pMwbAttr)
{
    int nRet = SGKS_SUCCESS;
    sgks_mpi_manager_s *mpi = NULL;
    void *ispHandle = NULL;

    mpi = mpi_get_managerHanle();
    if (mpi==NULL)
    {    
        ERR_PRINT("get mpi manager error!\n");
        return SGKS_FAIL;
    }

    ispHandle = mpi->ispToImageHandle;
    if (ispHandle==NULL)
    {    
        return SGKS_FAIL;
    }

    if(pMwbAttr == NULL)
    {
        return SGKS_ERR_ISP_PARAM_ERR;
    }

    nRet = gadi_isp_get_mwb_attr(ispHandle, pMwbAttr);
    if(nRet != SGKS_SUCCESS)
    {
        return SGKS_ERR_ISP_FUNCTION_ERR;
    }

    return nRet;
}

int sgks_mpi_isp_ResetAAAStats()
{
    int nRet = SGKS_SUCCESS;
    sgks_mpi_manager_s *mpi = NULL;
    void *ispHandle = NULL;

    mpi = mpi_get_managerHanle();
    if (mpi==NULL)
    {    
        ERR_PRINT("get mpi manager error!\n");
        return SGKS_FAIL;
    }

    ispHandle = mpi->ispToImageHandle;
    if (ispHandle==NULL)
    {    
        return SGKS_FAIL;
    }

    nRet = gadi_isp_reset_3a_static(ispHandle);
    if(nRet != SGKS_SUCCESS)
    {
        return SGKS_ERR_ISP_FUNCTION_ERR;
    }

    return nRet;    
}




